UNPKG

1.27 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var IS_PURE = require('../internals/is-pure');
4var NativePromise = require('../internals/native-promise-constructor');
5var getBuiltIn = require('../internals/get-built-in');
6var speciesConstructor = require('../internals/species-constructor');
7var promiseResolve = require('../internals/promise-resolve');
8var redefine = require('../internals/redefine');
9
10// `Promise.prototype.finally` method
11// https://tc39.github.io/ecma262/#sec-promise.prototype.finally
12$({ target: 'Promise', proto: true, real: true }, {
13 'finally': function (onFinally) {
14 var C = speciesConstructor(this, getBuiltIn('Promise'));
15 var isFunction = typeof onFinally == 'function';
16 return this.then(
17 isFunction ? function (x) {
18 return promiseResolve(C, onFinally()).then(function () { return x; });
19 } : onFinally,
20 isFunction ? function (e) {
21 return promiseResolve(C, onFinally()).then(function () { throw e; });
22 } : onFinally
23 );
24 }
25});
26
27// patch native Promise.prototype for native async functions
28if (!IS_PURE && typeof NativePromise == 'function' && !NativePromise.prototype['finally']) {
29 redefine(NativePromise.prototype, 'finally', getBuiltIn('Promise').prototype['finally']);
30}