1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var IS_PURE = require('../internals/is-pure');
|
---|
4 | var NativePromise = require('../internals/native-promise-constructor');
|
---|
5 | var fails = require('../internals/fails');
|
---|
6 | var getBuiltIn = require('../internals/get-built-in');
|
---|
7 | var speciesConstructor = require('../internals/species-constructor');
|
---|
8 | var promiseResolve = require('../internals/promise-resolve');
|
---|
9 | var redefine = require('../internals/redefine');
|
---|
10 |
|
---|
11 | // Safari bug https://bugs.webkit.org/show_bug.cgi?id=200829
|
---|
12 | var NON_GENERIC = !!NativePromise && fails(function () {
|
---|
13 | NativePromise.prototype['finally'].call({ then: function () { /* empty */ } }, function () { /* empty */ });
|
---|
14 | });
|
---|
15 |
|
---|
16 | // `Promise.prototype.finally` method
|
---|
17 | // https://tc39.es/ecma262/#sec-promise.prototype.finally
|
---|
18 | $({ target: 'Promise', proto: true, real: true, forced: NON_GENERIC }, {
|
---|
19 | 'finally': function (onFinally) {
|
---|
20 | var C = speciesConstructor(this, getBuiltIn('Promise'));
|
---|
21 | var isFunction = typeof onFinally == 'function';
|
---|
22 | return this.then(
|
---|
23 | isFunction ? function (x) {
|
---|
24 | return promiseResolve(C, onFinally()).then(function () { return x; });
|
---|
25 | } : onFinally,
|
---|
26 | isFunction ? function (e) {
|
---|
27 | return promiseResolve(C, onFinally()).then(function () { throw e; });
|
---|
28 | } : onFinally
|
---|
29 | );
|
---|
30 | }
|
---|
31 | });
|
---|
32 |
|
---|
33 | // makes sure that native promise-based APIs `Promise#finally` properly works with patched `Promise#then`
|
---|
34 | if (!IS_PURE && typeof NativePromise == 'function') {
|
---|
35 | var method = getBuiltIn('Promise').prototype['finally'];
|
---|
36 | if (NativePromise.prototype['finally'] !== method) {
|
---|
37 | redefine(NativePromise.prototype, 'finally', method, { unsafe: true });
|
---|
38 | }
|
---|
39 | }
|
---|