What happens when a promise is resolved multiple times

百般思念 提交于 2019-12-10 04:46:15

问题


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).


回答1:


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:

  1. Assert: F has a [[Promise]] internal slot whose value is an Object.
  2. Let promise be the value of F's [[Promise]] internal slot.
  3. Let alreadyResolved be the value of F's [[AlreadyResolved]] internal slot.
  4. If alreadyResolved.[[Value]] is true, return undefined.
  5. ...

(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.




回答2:


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, i.e. 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.



来源:https://stackoverflow.com/questions/43361492/what-happens-when-a-promise-is-resolved-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!