What is the standard behaviour if a ES6 promise is rejected / resolved multiple times?
The following code was only resolved once in Google Chrome
, is this the standard behaviour in all browsers?
new Promise(function(e) {
$('#button').click(function(){
resolve();
});
});
I have seen a promise polyfill throwing an exception on trying to resolve an already resolved promise. Does the specification for es6-promise specify this, or isn't the polyfill standard compliant?
Update
Sorry, I just realized that it is not a polyfill, but just a minimal implementation of a Promise (non standard).
A promise cannot be resolved more than once. Once the promise is settled (resolved or rejected), calls to settle (resolve or reject) it again are ignored. No error is raised.
I have seen a promise polyfill throwing an exception on trying to resolve an already resolved promise. Does the specification for es6-promise specify this...?
No, it does not. This is covered in Promise Resolve Functions , which says what they do. Here are the first four steps:
When a promise resolve function F is called with argument resolution, the following steps are taken:
- Assert: F has a [[Promise]] internal slot whose value is an Object.
- Let promise be the value of F's [[Promise]] internal slot.
- Let alreadyResolved be the value of F's [[AlreadyResolved]] internal slot.
- If alreadyResolved.[[Value]] is true, return undefined.
- ...
(my emphasis)
There are people who argue that attempting to settle a settled promise should be an error. Since that didn't make it into the spec for ES2015, it will probably be hard to add it, even if they could find a TC-39 champion for a proposal to do so. It would probably end up having to be a flag or Promise
subclass, and one can readily add one's own Promise
subclass if this is the behavior one wants.
According to the ECMAScript 2015 Language Spec
Promise Objects
A Promise is an object that is used as a placeholder for the eventual results of a deferred (and possibly asynchronous) computation.
Any Promise object is in one of three mutually exclusive states: fulfilled, rejected, and pending: A promise p is fulfilled if p.then(f, r) will immediately enqueue a Job to call the function f. A promise p is rejected if p.then(f, r) will immediately enqueue a Job to call the function r. A promise is pending if it is neither fulfilled nor rejected. A promise is said to be settled if it is not pending, ie if it is either fulfilled or rejected.
A promise is resolved if it is settled or if it has been “locked in” to match the state of another promise. Attempting to resolve or reject a resolved promise has no effect. A promise is unresolved if it is not resolved. An unresolved promise is always in the pending state. A resolved promise may be pending, fulfilled or rejected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.