Promise.js (2167B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Promise.prototype.finally proposal, stage 3. 6 // Promise.prototype.finally ( onFinally ) 7 function Promise_finally(onFinally) { 8 // Step 1. 9 var promise = this; 10 11 // Step 2. 12 if (!IsObject(promise)) { 13 ThrowTypeError(JSMSG_INCOMPATIBLE_PROTO, "Promise", "finally", "value"); 14 } 15 16 // Step 3. 17 var C = SpeciesConstructor(promise, GetBuiltinConstructor("Promise")); 18 19 // Step 4. 20 assert(IsConstructor(C), "SpeciesConstructor returns a constructor function"); 21 22 // Steps 5-6. 23 var thenFinally, catchFinally; 24 if (!IsCallable(onFinally)) { 25 thenFinally = onFinally; 26 catchFinally = onFinally; 27 } else { 28 // ThenFinally Function. 29 // The parentheses prevent the infering of a function name. 30 // prettier-ignore 31 (thenFinally) = function(value) { 32 // Steps 1-2 (implicit). 33 34 // Step 3. 35 var result = callContentFunction(onFinally, undefined); 36 37 // Steps 4-5 (implicit). 38 39 // Step 6. 40 var promise = PromiseResolve(C, result); 41 42 // Step 7. 43 // FIXME: spec issue - "be equivalent to a function that" is not a defined spec term. 44 // https://github.com/tc39/ecma262/issues/933 45 46 // Step 8. 47 return callContentFunction(promise.then, promise, function() { 48 return value; 49 }); 50 }; 51 52 // CatchFinally Function. 53 // prettier-ignore 54 (catchFinally) = function(reason) { 55 // Steps 1-2 (implicit). 56 57 // Step 3. 58 var result = callContentFunction(onFinally, undefined); 59 60 // Steps 4-5 (implicit). 61 62 // Step 6. 63 var promise = PromiseResolve(C, result); 64 65 // Step 7. 66 // FIXME: spec issue - "be equivalent to a function that" is not a defined spec term. 67 // https://github.com/tc39/ecma262/issues/933 68 69 // Step 8. 70 return callContentFunction(promise.then, promise, function() { 71 throw reason; 72 }); 73 }; 74 } 75 76 // Step 7. 77 return callContentFunction(promise.then, promise, thenFinally, catchFinally); 78 }