[promises] resolver's resolved flag check on promise callback

Yutaka Hirano <[email protected]>
Newsgroups gmane.comp.web.dom.general
Message-ID <CABihn6HdZKYUu2Ucv8h0VU+F9dYsnSHMtYSaozSmUmSO7oEfAg@mail.gmail.com>
Hi, I am implementing promises and I have a question.
In resolve algorithm, we create promise callbacks for the given resolver
and pass them to a (possibly) user defined function.
A user can call them more than once.
Since a promise callback for |resolver| and |algorithm| runs |resolver|'s
|algorithm| and the |algorithm| doesn't checks |resolver|'s resolved flag,
it seems that the promise can be resolved or rejected multiple times.
Is it correct? I think the check for the resolved flag is needed in at
least:
 - promise callback
 - 4.4 in resolve algorithm.

Example:

var resolver;
var promise = new Promise(function(r) {resolver = r;});
promise.then(function(result) {
  console.log('FULFILLED: result = ' + result);
}, function(result) {
  console.log('REJECTED: result = ' + result);
});

var value = {
  then: function (resolvePromiseCallback, rejectPromiseCallback) {
    // We can resolve the promise, of course.
    resolvePromiseCallback(1);
    // Can we resolve it again?
    resolvePromiseCallback(2);
    // Can we reject it after resolving it?
    rejectPromiseCallback(3);
    // If we throw an exception, will the promise be rejected again?
    throw 4;
  }
};

resolver.resolve(value);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.