debian-mirror-gitlab/core-js/modules/es.promise.finally.js

37 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-09-25 13:30:05 +05:30
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var NativePromise = require('../internals/native-promise-constructor');
2019-12-04 20:38:33 +05:30
var fails = require('../internals/fails');
2019-09-25 13:30:05 +05:30
var getBuiltIn = require('../internals/get-built-in');
var speciesConstructor = require('../internals/species-constructor');
var promiseResolve = require('../internals/promise-resolve');
var redefine = require('../internals/redefine');
2019-12-04 20:38:33 +05:30
// Safari bug https://bugs.webkit.org/show_bug.cgi?id=200829
var NON_GENERIC = !!NativePromise && fails(function () {
NativePromise.prototype['finally'].call({ then: function () { /* empty */ } }, function () { /* empty */ });
});
2019-09-25 13:30:05 +05:30
// `Promise.prototype.finally` method
// https://tc39.github.io/ecma262/#sec-promise.prototype.finally
2019-12-04 20:38:33 +05:30
$({ target: 'Promise', proto: true, real: true, forced: NON_GENERIC }, {
2019-09-25 13:30:05 +05:30
'finally': function (onFinally) {
var C = speciesConstructor(this, getBuiltIn('Promise'));
var isFunction = typeof onFinally == 'function';
return this.then(
isFunction ? function (x) {
return promiseResolve(C, onFinally()).then(function () { return x; });
} : onFinally,
isFunction ? function (e) {
return promiseResolve(C, onFinally()).then(function () { throw e; });
} : onFinally
);
}
});
// patch native Promise.prototype for native async functions
if (!IS_PURE && typeof NativePromise == 'function' && !NativePromise.prototype['finally']) {
redefine(NativePromise.prototype, 'finally', getBuiltIn('Promise').prototype['finally']);
}