Skip to content

Commit

Permalink
make .constructor === Promise work with polyfilled Promise for al…
Browse files Browse the repository at this point in the history
…l native promise-based APIs
  • Loading branch information
zloirock committed Apr 27, 2021
1 parent 02ed1bd commit a09e802
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Changelog
##### Unreleased
- Made `instanceof Promise` work with polyfilled `Promise` for all native promise-based APIs
- Made `instanceof Promise` and `.constructor === Promise` work with polyfilled `Promise` for all native promise-based APIs

##### 3.11.0 - 2021.04.22
- Added [accessible `Object#hasOwnProperty` stage 2 proposal](https://github.com/tc39/proposal-accessible-object-hasownproperty)
Expand Down
27 changes: 12 additions & 15 deletions packages/core-js/modules/es.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ var PROMISE = 'Promise';
var getInternalState = InternalStateModule.get;
var setInternalState = InternalStateModule.set;
var getInternalPromiseState = InternalStateModule.getterFor(PROMISE);
var NativePromisePrototype = NativePromise && NativePromise.prototype;
var PromiseConstructor = NativePromise;
var TypeError = global.TypeError;
var document = global.document;
var process = global.process;
var $fetch = getBuiltIn('fetch');
var newPromiseCapability = newPromiseCapabilityModule.f;
var newGenericPromiseCapability = newPromiseCapability;
var DISPATCH_EVENT = !!(document && document.createEvent && global.dispatchEvent);
Expand Down Expand Up @@ -284,30 +284,27 @@ if (FORCED) {
: newGenericPromiseCapability(C);
};

if (!IS_PURE && typeof NativePromise == 'function') {
nativeThen = NativePromise.prototype.then;
if (!IS_PURE && typeof NativePromise == 'function' && NativePromisePrototype !== Object.prototype) {
nativeThen = NativePromisePrototype.then;

// wrap native Promise#then for native async functions
redefine(NativePromise.prototype, 'then', function then(onFulfilled, onRejected) {
// make `Promise#then` return a polyfilled `Promise` for native promise-based APIs
redefine(NativePromisePrototype, 'then', function then(onFulfilled, onRejected) {
var that = this;
return new PromiseConstructor(function (resolve, reject) {
nativeThen.call(that, resolve, reject);
}).then(onFulfilled, onRejected);
// https://github.com/zloirock/core-js/issues/640
}, { unsafe: true });

// make `instanceof Promise` work for all native promise-based APIs
// make `.constructor === Promise` work for native promise-based APIs
try {
delete NativePromisePrototype.constructor;
} catch (error) { /* empty */ }

// make `instanceof Promise` work for native promise-based APIs
if (setPrototypeOf) {
setPrototypeOf(NativePromise.prototype, PromiseConstructor.prototype);
setPrototypeOf(NativePromisePrototype, PromiseConstructor.prototype);
}

// wrap fetch result, TODO: drop it in `core-js@4` in favor of workaround above
if (typeof $fetch == 'function') $({ global: true, enumerable: true, forced: true }, {
// eslint-disable-next-line no-unused-vars -- required for `.length`
fetch: function fetch(input /* , init */) {
return promiseResolve(PromiseConstructor, $fetch.apply(global, arguments));
}
});
}
}

Expand Down

0 comments on commit a09e802

Please sign in to comment.