Skip to content

Commit

Permalink
trivial change to make assimilation of thenables lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
bergus committed Aug 6, 2014
1 parent 43031ad commit f36cab5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions promise/Promise.js
Expand Up @@ -372,12 +372,14 @@ Promise.method = function makeThenHandler(fn, warn) {
if (typeof then != "function") // A+ 2.3.3.4 "If then is not a function, fulfill promise with x"
return Promise.of(v);
return new Promise(function thenableResolver(fulfill, reject, progress) {
try {
// A+ 2.3.3.3 "call then with x as this, first argument resolvePromise, and second argument rejectPromise"
then.call(v, fulfill.async, reject.async, progress); // TODO: support cancellation
} catch(e) { // A+ 2.3.3.3.4 "If calling then throws an exception e"
reject.async(e); // "reject promise with e as the reason (unless already resolved)"
}
return function advanceThenableResolution() { // Lazyness!
try {
// A+ 2.3.3.3 "call then with x as this, first argument resolvePromise, and second argument rejectPromise"
then.call(v, fulfill.async, reject.async, progress); // TODO: support cancellation
} catch(e) { // A+ 2.3.3.3.4 "If calling then throws an exception e"
reject.async(e); // "reject promise with e as the reason (unless already resolved)"
}
};
}).chain(Promise.resolve); // A+ 2.3.3.3.1 "when resolvePromise is called with a value y, run [[Resolve]](promise, y)" (recursively)
};
};
Expand Down

0 comments on commit f36cab5

Please sign in to comment.