Skip to content

Commit

Permalink
add feature detection to Promise.try
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 31, 2024
1 parent 19b1a68 commit 8b10ea8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/core-js/modules/esnext.promise.try.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
'use strict';
var $ = require('../internals/export');
var globalThis = require('../internals/global-this');
var apply = require('../internals/function-apply');
var slice = require('../internals/array-slice');
var newPromiseCapabilityModule = require('../internals/new-promise-capability');
var aCallable = require('../internals/a-callable');
var perform = require('../internals/perform');

var Promise = globalThis.Promise;

var ACCEPT_ARGUMENTS = false;
// Avoiding the use of polyfills of the previous iteration of this proposal
// that does not accept arguments of the callback
var FORCED = !Promise || !Promise['try'] || perform(function () {
Promise['try'](function (argument) {
ACCEPT_ARGUMENTS = argument === 8;
}, 8);
}).error || !ACCEPT_ARGUMENTS;

// `Promise.try` method
// https://github.com/tc39/proposal-promise-try
$({ target: 'Promise', stat: true }, {
$({ target: 'Promise', stat: true, forced: FORCED }, {
'try': function (callbackfn /* , ...args */) {
var args = arguments.length > 1 ? slice(arguments, 1) : [];
var promiseCapability = newPromiseCapabilityModule.f(this);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.regexp.escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var escapeChar = function (chr) {
};

// Avoiding the use of polyfills of the previous iteration of this proposal
var FORCED = !!$escape && $escape('ab') !== '\\x61b';
var FORCED = !$escape || $escape('ab') !== '\\x61b';

// `RegExp.escape` method
// https://github.com/tc39/proposal-regex-escaping
Expand Down
6 changes: 5 additions & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,11 @@ GLOBAL.tests = {
return Number.fromString;
},
'esnext.promise.try': [PROMISES_SUPPORT, function () {
return Promise['try'];
var ACCEPT_ARGUMENTS = false;
Promise['try'](function (argument) {
ACCEPT_ARGUMENTS = argument === 8;
}, 8);
return ACCEPT_ARGUMENTS;
}],
'esnext.regexp.escape': function () {
return RegExp.escape('ab') === '\\x61b';
Expand Down

0 comments on commit 8b10ea8

Please sign in to comment.