diff options
author | Matt A. Tobin <email@mattatobin.com> | 2019-11-13 05:54:58 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2019-11-13 05:54:58 -0500 |
commit | 9308ec68e863e4c6e650680370a5d7baa9f0d1f3 (patch) | |
tree | c70b2b78369b4554cdeb0db2dd0161b9945d6c21 /js/src/builtin/Promise.js | |
parent | ecdfbc692322ccc0f2dbc8811fee8282cf125b3b (diff) | |
parent | ce0dd36a78814c59950fde6c19413c1f7ea85ee1 (diff) | |
download | UXP-9308ec68e863e4c6e650680370a5d7baa9f0d1f3.tar UXP-9308ec68e863e4c6e650680370a5d7baa9f0d1f3.tar.gz UXP-9308ec68e863e4c6e650680370a5d7baa9f0d1f3.tar.lz UXP-9308ec68e863e4c6e650680370a5d7baa9f0d1f3.tar.xz UXP-9308ec68e863e4c6e650680370a5d7baa9f0d1f3.zip |
Merge branch 'master' into mailnews-work
Diffstat (limited to 'js/src/builtin/Promise.js')
-rw-r--r-- | js/src/builtin/Promise.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/js/src/builtin/Promise.js b/js/src/builtin/Promise.js index 704cbab0b..91a1e1f56 100644 --- a/js/src/builtin/Promise.js +++ b/js/src/builtin/Promise.js @@ -14,3 +14,72 @@ function Promise_catch(onRejected) { // Steps 1-2. return callContentFunction(this.then, this, undefined, onRejected); } + +// Promise.prototype.finally(onFinally) +// See https://tc39.es/proposal-promise-finally/ +function Promise_finally(onFinally) { + // Step 1. + var promise = this; + + // Step 2. + if (!IsObject(promise)) + ThrowTypeError(JSMSG_INCOMPATIBLE_PROTO, "Promise", "finally", "value"); + + // Step 3. + var C = SpeciesConstructor(promise, GetBuiltinConstructor("Promise")); + + // Step 4. + assert(IsConstructor(C), "SpeciesConstructor returns a constructor function"); + + // Steps 5-6. + var thenFinally, catchFinally; + if (!IsCallable(onFinally)) { + thenFinally = onFinally; + catchFinally = onFinally; + } else { + // ThenFinally Function. + // The parentheses prevent the inferring of a function name. + (thenFinally) = function(value) { + // Steps 1-2 (implicit). + + // Step 3. + var result = onFinally(); + + // Steps 4-5 (implicit). + + // Step 6. + var promise = PromiseResolve(C, result); + + // Step 7. + // FIXME: spec issue - "be equivalent to a function that" is not a defined spec term. + // https://github.com/tc39/ecma262/issues/933 + + // Step 8. + return callContentFunction(promise.then, promise, function() { return value; }); + }; + + // CatchFinally Function. + // The parentheses prevent the inferring of a function name. + (catchFinally) = function(reason) { + // Steps 1-2 (implicit). + + // Step 3. + var result = onFinally(); + + // Steps 4-5 (implicit). + + // Step 6. + var promise = PromiseResolve(C, result); + + // Step 7. + // FIXME: spec issue - "be equivalent to a function that" is not a defined spec term. + // https://github.com/tc39/ecma262/issues/933 + + // Step 8. + return callContentFunction(promise.then, promise, function() { throw reason; }); + }; + } + + // Step 7. + return callContentFunction(promise.then, promise, thenFinally, catchFinally); +} |