This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
polyfill.js
45 lines (43 loc) · 1.52 KB
/
polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
if (typeof Promise !== 'function') {
throw new TypeError('A global Promise is required');
}
if (typeof Promise.prototype.finally !== 'function') {
var speciesConstructor = function (O, defaultConstructor) {
if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
throw new TypeError('Assertion failed: Type(O) is not Object');
}
var C = O.constructor;
if (typeof C === 'undefined') {
return defaultConstructor;
}
if (!C || (typeof C !== 'object' && typeof C !== 'function')) {
throw new TypeError('O.constructor is not an Object');
}
var S = typeof Symbol === 'function' && typeof Symbol.species === 'symbol' ? C[Symbol.species] : undefined;
if (S == null) {
return defaultConstructor;
}
if (typeof S === 'function' && S.prototype) {
return S;
}
throw new TypeError('no constructor found');
};
var shim = {
finally(onFinally) {
var promise = this;
if (typeof promise !== 'object' || promise === null) {
throw new TypeError('"this" value is not an Object');
}
var C = speciesConstructor(promise, Promise); // throws if SpeciesConstructor throws
if (typeof onFinally !== 'function') {
return Promise.prototype.then.call(promise, onFinally, onFinally);
}
return Promise.prototype.then.call(
promise,
x => new C(resolve => resolve(onFinally())).then(() => x),
e => new C(resolve => resolve(onFinally())).then(() => { throw e; })
);
}
};
Object.defineProperty(Promise.prototype, 'finally', { configurable: true, writable: true, value: shim.finally });
}