Skip to content

Commit

Permalink
polyfill Promise with unhandledrejection event support in Deno < …
Browse files Browse the repository at this point in the history
…1.24
  • Loading branch information
zloirock committed Jul 22, 2022
1 parent 3ddbb51 commit c08f5e6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
20 changes: 20 additions & 0 deletions packages/core-js-compat/src/data.mjs
Expand Up @@ -924,42 +924,56 @@ export const data = {
'es.promise': {
// V8 6.6 has a serious bug
chrome: '67', // '51',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
safari: '11.0',
rhino: '1.7.14',
},
'es.promise.constructor': {
// V8 6.6 has a serious bug
chrome: '67', // '51',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
safari: '11.0',
rhino: '1.7.14',
},
'es.promise.all': {
chrome: '67',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
safari: '11.0',
rhino: '1.7.14',
},
'es.promise.all-settled': {
chrome: '76',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '71',
safari: '13',
},
'es.promise.any': {
chrome: '85',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '79',
safari: '14.0',
},
'es.promise.catch': {
chrome: '67',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
safari: '11.0',
rhino: '1.7.14',
},
'es.promise.finally': {
// V8 6.6 has a serious bug
chrome: '67', // '63',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
// Previous versions are non-generic
// https://bugs.webkit.org/show_bug.cgi?id=200788
Expand All @@ -969,18 +983,24 @@ export const data = {
},
'es.promise.race': {
chrome: '67',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
safari: '11.0',
rhino: '1.7.14',
},
'es.promise.reject': {
chrome: '67',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
safari: '11.0',
rhino: '1.7.14',
},
'es.promise.resolve': {
chrome: '67',
// `unhandledrejection` event support was added in Deno@1.24
deno: '1.24',
firefox: '69',
safari: '11.0',
rhino: '1.7.14',
Expand Down
23 changes: 12 additions & 11 deletions packages/core-js/internals/promise-constructor-detection.js
Expand Up @@ -5,6 +5,7 @@ var isForced = require('../internals/is-forced');
var inspectSource = require('../internals/inspect-source');
var wellKnownSymbol = require('../internals/well-known-symbol');
var IS_BROWSER = require('../internals/engine-is-browser');
var IS_DENO = require('../internals/engine-is-deno');
var IS_PURE = require('../internals/is-pure');
var V8_VERSION = require('../internals/engine-v8-version');

Expand All @@ -25,18 +26,18 @@ var FORCED_PROMISE_CONSTRUCTOR = isForced('Promise', function () {
// We can't use @@species feature detection in V8 since it causes
// deoptimization and performance degradation
// https://github.com/zloirock/core-js/issues/679
if (V8_VERSION >= 51 && /native code/.test(PROMISE_CONSTRUCTOR_SOURCE)) return false;
// Detect correctness of subclassing with @@species support
var promise = new NativePromiseConstructor(function (resolve) { resolve(1); });
var FakePromise = function (exec) {
exec(function () { /* empty */ }, function () { /* empty */ });
};
var constructor = promise.constructor = {};
constructor[SPECIES] = FakePromise;
SUBCLASSING = promise.then(function () { /* empty */ }) instanceof FakePromise;
if (!SUBCLASSING) return true;
if (V8_VERSION < 51 || !/native code/.test(PROMISE_CONSTRUCTOR_SOURCE)) {
// Detect correctness of subclassing with @@species support
var promise = new NativePromiseConstructor(function (resolve) { resolve(1); });
var FakePromise = function (exec) {
exec(function () { /* empty */ }, function () { /* empty */ });
};
var constructor = promise.constructor = {};
constructor[SPECIES] = FakePromise;
SUBCLASSING = promise.then(function () { /* empty */ }) instanceof FakePromise;
if (!SUBCLASSING) return true;
// Unhandled rejections tracking support, NodeJS Promise without it fails @@species test
return !GLOBAL_CORE_JS_PROMISE && IS_BROWSER && !NATIVE_PROMISE_REJECTION_EVENT;
} return !GLOBAL_CORE_JS_PROMISE && (IS_BROWSER || IS_DENO) && !NATIVE_PROMISE_REJECTION_EVENT;
});

module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion tests/compat/tests.js
Expand Up @@ -33,6 +33,8 @@ if (!V8_VERSION && USERAGENT) {

var IS_BROWSER = typeof window == 'object' && typeof Deno != 'object';

var IS_DENO = typeof Deno == 'object' && Deno && typeof Deno.version == 'object';

// var IS_NODE = Object.prototype.toString.call(process) == '[object process]';

var WEBKIT_STRING_PAD_BUG = /Version\/10(?:\.\d+){1,2}(?: [\w./]+)?(?: Mobile\/\w+)? Safari\//.test(USERAGENT);
Expand All @@ -59,7 +61,7 @@ var PROMISES_SUPPORT = function () {

return promise.then(empty) instanceof FakePromise
&& V8_VERSION !== 66
&& (!IS_BROWSER || typeof PromiseRejectionEvent == 'function');
&& (!(IS_BROWSER || IS_DENO) || typeof PromiseRejectionEvent == 'function');
};

var PROMISE_STATICS_ITERATION = function () {
Expand Down

0 comments on commit c08f5e6

Please sign in to comment.