Skip to content

Commit

Permalink
move Promise.try to stable ES
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 9, 2024
1 parent a51fc0d commit 71d37cb
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 86 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
- `Iterator.prototype[@@toStringTag]`
- Moved to stable ES, [October 2024 TC39 meeting](https://github.com/tc39/proposal-iterator-helpers/issues/284#event-14549961807)
- Added `es.` namespace modules, `/es/` and `/stable/` namespaces entries
- [`Promise.try`](https://github.com/tc39/proposal-promise-try):
- Built-ins:
- `Promise.try`
- Moved to stable ES, October 2024 TC39 meeting
- Added `es.` namespace module, `/es/` and `/stable/` namespaces entries
- Fixed `/actual|full/promise/try` entries for the callback arguments support
- [`Math.sumPrecise` proposal](https://github.com/tc39/proposal-math-sum/):
- Built-ins:
- `Math.sumPrecise`
- Moved to stage 3, [October 2024 TC39 meeting](https://x.com/robpalmer2/status/1843829675036160179)
- Added `/actual/` namespace entries, unconditional forced replacement changed to feature detection
- Fixed `core-js-pure/actual|full/promise/try` entries for the callback arguments support
- Compat data improvements:
- [`JSON.parse` source text access proposal](https://github.com/tc39/proposal-json-parse-with-source) features marked as [supported from FF132](https://bugzilla.mozilla.org/show_bug.cgi?id=1913085)
- `self` descriptor [is fixed](https://github.com/denoland/deno/issues/24683) in Deno 1.46.0
Expand Down
58 changes: 30 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
- [`Promise.allSettled`](#promiseallsettled)
- [`Promise.any`](#promiseany)
- [`Promise.prototype.finally`](#promiseprototypefinally)
- [`Promise.try`](#promisetry)
- [`Promise.withResolvers`](#promisewithresolvers)
- [`Symbol.asyncIterator` for asynchronous iteration](#symbolasynciterator-for-asynchronous-iteration)
- [`Symbol.prototype.description`](#symbolprototypedescription)
Expand All @@ -163,7 +164,6 @@ structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
- [`Float16` methods](#float16-methods)
- [`Uint8Array` to / from base64 and hex](#uint8array-to--from-base64-and-hex)
- [Explicit resource management](#explicit-resource-management)
- [`Promise.try`](#promisetry)
- [`RegExp` escaping](#regexp-escaping)
- [`Math.sumPrecise`](#mathsumprecise)
- [`Symbol.metadata` for decorators metadata proposal](#symbolmetadata-for-decorators-metadata-proposal)
Expand Down Expand Up @@ -1206,8 +1206,9 @@ core-js(-pure)/es|stable|actual|full/date/to-primitive
```js
new Date(NaN).toString(); // => 'Invalid Date'
```

#### ECMAScript: Promise[](#index)
Modules [`es.promise`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.js), [`es.promise.all-settled`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.all-settled.js), [`es.promise.any`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.any.js), [`es.promise.finally`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.finally.js) and [`es.promise.with-resolvers`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.with-resolvers.js).
Modules [`es.promise`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.js), [`es.promise.all-settled`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.all-settled.js), [`es.promise.any`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.any.js), [`es.promise.finally`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.finally.js), [`es.promise.try`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.try.js) and [`es.promise.with-resolvers`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.promise.with-resolvers.js).
```ts
class Promise {
constructor(executor: (resolve: Function, reject: Function) => void): Promise;
Expand All @@ -1220,6 +1221,7 @@ class Promise {
static race(iterable: Iterable): Promise;
static reject(r: any): Promise;
static resolve(x: any): Promise;
static try(callbackfn: Function, ...args?: Array<mixed>): Promise;
static withResolvers(): { promise: Promise, resolve: function, reject: function };
}
```
Expand All @@ -1229,6 +1231,7 @@ core-js(-pure)/es|stable|actual|full/promise
core-js(-pure)/es|stable|actual|full/promise/all-settled
core-js(-pure)/es|stable|actual|full/promise/any
core-js(-pure)/es|stable|actual|full/promise/finally
core-js(-pure)/es|stable|actual|full/promise/try
core-js(-pure)/es|stable|actual|full/promise/with-resolvers
```
Basic [*example*](https://tinyurl.com/23bhbhbu):
Expand Down Expand Up @@ -1310,6 +1313,19 @@ Promise.any([
Promise.reject(3),
]).catch(({ errors }) => console.log(errors)); // => [1, 2, 3]
```
`Promise.try` [*examples*](https://tinyurl.com/2p48ojau):
```js
/* eslint-disable promise/prefer-await-to-callbacks -- example */
Promise.try(() => 42).then(it => console.log(`Promise, resolved as ${ it }`));

Promise.try(() => { throw new Error('42'); }).catch(error => console.log(`Promise, rejected as ${ error }`));

Promise.try(async () => 42).then(it => console.log(`Promise, resolved as ${ it }`));

Promise.try(async () => { throw new Error('42'); }).catch(error => console.log(`Promise, rejected as ${ error }`));

Promise.try(it => it, 42).then(it => console.log(`Promise, resolved as ${ it }`));
```
`Promise.withResolvers` [*examples*](https://tinyurl.com/2gx4t3xu):
```js
const d = Promise.withResolvers();
Expand Down Expand Up @@ -2299,6 +2315,18 @@ class Promise {
```
core-js/proposals/promise-finally
```

##### [`Promise.try`](https://github.com/tc39/proposal-promise-try)
```ts
class Promise {
static try(callbackfn: Function, ...args?: Array<mixed>): Promise;
}
```
[*CommonJS entry points:*](#commonjs-api)
```
core-js/proposals/promise-try
```

##### [`Promise.withResolvers`](https://github.com/tc39/proposal-promise-with-resolvers)[](#index)
```ts
class Promise {
Expand Down Expand Up @@ -2552,32 +2580,6 @@ core-js(-pure)/actual|full/iterator/dispose
core-js(-pure)/actual|full/async-iterator/async-dispose
```

##### [`Promise.try`](https://github.com/tc39/proposal-promise-try)
Module [`esnext.promise.try`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.promise.try.js)
```ts
class Promise {
static try(callbackfn: Function, ...args?: Array<mixed>): Promise;
}
```
[*CommonJS entry points:*](#commonjs-api)
```
core-js/proposals/promise-try
core-js(-pure)/actual|full/promise/try
```
[*Examples*](https://tinyurl.com/2p48ojau):
```js
/* eslint-disable promise/prefer-await-to-callbacks -- example */
Promise.try(() => 42).then(it => console.log(`Promise, resolved as ${ it }`));

Promise.try(() => { throw new Error('42'); }).catch(error => console.log(`Promise, rejected as ${ error }`));

Promise.try(async () => 42).then(it => console.log(`Promise, resolved as ${ it }`));

Promise.try(async () => { throw new Error('42'); }).catch(error => console.log(`Promise, rejected as ${ error }`));

Promise.try(it => it, 42).then(it => console.log(`Promise, resolved as ${ it }`));
```

##### [`RegExp` escaping](https://github.com/tc39/proposal-regex-escaping)[](#index)
Module [`esnext.regexp.escape`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.regexp.escape.js)
```ts
Expand Down
11 changes: 7 additions & 4 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,10 @@ export const data = {
rhino: '1.7.14',
safari: '11.0',
},
'es.promise.try': {
bun: '1.1.22',
chrome: '128',
},
'es.promise.with-resolvers': {
bun: '0.7.1',
chrome: '119',
Expand Down Expand Up @@ -2387,10 +2391,8 @@ export const data = {
'esnext.promise.all-settled': null,
// TODO: Remove from `core-js@4`
'esnext.promise.any': null,
'esnext.promise.try': {
bun: '1.1.22',
chrome: '128',
},
// TODO: Remove from `core-js@4`
'esnext.promise.try': null,
// TODO: Remove from `core-js@4`
'esnext.promise.with-resolvers': null,
// TODO: Remove from `core-js@4`
Expand Down Expand Up @@ -2879,6 +2881,7 @@ export const renamed = new Map([
['esnext.object.group-by', 'es.object.group-by'],
['esnext.promise.all-settled', 'es.promise.all-settled'],
['esnext.promise.any', 'es.promise.any'],
['esnext.promise.try', 'es.promise.try'],
['esnext.promise.with-resolvers', 'es.promise.with-resolvers'],
['esnext.set.difference.v2', 'es.set.difference.v2'],
['esnext.set.intersection.v2', 'es.set.intersection.v2'],
Expand Down
1 change: 1 addition & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,6 @@ export default {
'es.iterator.some',
'es.iterator.take',
'es.iterator.to-array',
'es.promise.try',
],
};
1 change: 1 addition & 0 deletions packages/core-js/actual/promise/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var parent = require('../../stable/promise');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.promise.try');
require('../../modules/esnext.promise.with-resolvers');

Expand Down
14 changes: 3 additions & 11 deletions packages/core-js/actual/promise/try.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
'use strict';
require('../../modules/es.promise');
var parent = require('../../stable/promise/try');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.promise.try');
var apply = require('../../internals/function-apply');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Promise = path.Promise;
var promiseTry = Promise['try'];

// eslint-disable-next-line no-unused-vars -- required for arity
module.exports = ({ 'try': function (callbackfn /* , ...args */) {
return apply(promiseTry, isCallable(this) ? this : Promise, arguments);
} })['try'];
module.exports = parent;
1 change: 1 addition & 0 deletions packages/core-js/es/promise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.promise.all-settled');
require('../../modules/es.promise.any');
require('../../modules/es.promise.try');
require('../../modules/es.promise.with-resolvers');
require('../../modules/es.promise.finally');
require('../../modules/es.string.iterator');
Expand Down
15 changes: 15 additions & 0 deletions packages/core-js/es/promise/try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.promise.try');
var apply = require('../../internals/function-apply');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Promise = path.Promise;
var $try = Promise['try'];

// eslint-disable-next-line no-unused-vars -- required for arity
module.exports = ({ 'try': function (callbackfn /* , ...args */) {
return apply($try, isCallable(this) ? this : Promise, arguments);
} })['try'];
33 changes: 33 additions & 0 deletions packages/core-js/modules/es.promise.try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'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, forced: FORCED }, {
'try': function (callbackfn /* , ...args */) {
var args = arguments.length > 1 ? slice(arguments, 1) : [];
var promiseCapability = newPromiseCapabilityModule.f(this);
var result = perform(function () {
return apply(aCallable(callbackfn), undefined, args);
});
(result.error ? promiseCapability.reject : promiseCapability.resolve)(result.value);
return promiseCapability.promise;
}
});
34 changes: 2 additions & 32 deletions packages/core-js/modules/esnext.promise.try.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
'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, forced: FORCED }, {
'try': function (callbackfn /* , ...args */) {
var args = arguments.length > 1 ? slice(arguments, 1) : [];
var promiseCapability = newPromiseCapabilityModule.f(this);
var result = perform(function () {
return apply(aCallable(callbackfn), undefined, args);
});
(result.error ? promiseCapability.reject : promiseCapability.resolve)(result.value);
return promiseCapability.promise;
}
});
// TODO: Remove from `core-js@4`
require('../modules/es.promise.try.js');
4 changes: 4 additions & 0 deletions packages/core-js/stable/promise/try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
var parent = require('../../es/promise/try');

module.exports = parent;
1 change: 0 additions & 1 deletion packages/core-js/stage/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require('../proposals/explicit-resource-management');
require('../proposals/float16');
require('../proposals/json-parse-with-source');
require('../proposals/math-sum');
require('../proposals/promise-try');
require('../proposals/regexp-escaping');
// TODO: Obsolete versions, remove from `core-js@4`
require('../proposals/array-grouping-stage-3');
Expand Down
1 change: 1 addition & 0 deletions packages/core-js/stage/4.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require('../proposals/global-this');
require('../proposals/iterator-helpers-stage-3-2');
require('../proposals/promise-all-settled');
require('../proposals/promise-any');
require('../proposals/promise-try');
require('../proposals/promise-with-resolvers');
require('../proposals/relative-indexing-method');
require('../proposals/set-methods-v2');
Expand Down
14 changes: 7 additions & 7 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,13 @@ GLOBAL.tests = {
}],
'es.promise.reject': PROMISES_SUPPORT,
'es.promise.resolve': PROMISES_SUPPORT,
'es.promise.try': [PROMISES_SUPPORT, function () {
var ACCEPT_ARGUMENTS = false;
Promise['try'](function (argument) {
ACCEPT_ARGUMENTS = argument === 8;
}, 8);
return ACCEPT_ARGUMENTS;
}],
'es.promise.with-resolvers': [PROMISES_SUPPORT, function () {
return Promise.withResolvers;
}],
Expand Down Expand Up @@ -1773,13 +1780,6 @@ GLOBAL.tests = {
'esnext.number.from-string': function () {
return Number.fromString;
},
'esnext.promise.try': [PROMISES_SUPPORT, function () {
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
2 changes: 1 addition & 1 deletion tests/entries/unit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(load(NS, 'promise/all-settled')([1, 2, 3]) instanceof Promise);
ok(load(NS, 'promise/any')([1, 2, 3]) instanceof Promise);
ok(load(NS, 'promise/finally')(new Promise(resolve => resolve), it => it) instanceof Promise);
ok(load(NS, 'promise/try')(() => 42) instanceof Promise);
ok(load(NS, 'promise/with-resolvers')().promise instanceof load(NS, 'promise'));
ok(load(NS, 'is-iterable')([]));
ok(typeof load(NS, 'get-iterator-method')([]) == 'function');
Expand Down Expand Up @@ -703,7 +704,6 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(typeof load(NS, 'json/raw-json')(42) == 'object');
ok(load(NS, 'math/f16round')(1.337) === 1.3369140625);
ok(load(NS, 'math/sum-precise')([1, 2, 3]) === 6);
ok(load(NS, 'promise/try')(() => 42) instanceof load(NS, 'promise'));
ok(load(NS, 'regexp/escape')('10$') === '\\x310\\$');
ok(load(NS, 'symbol/dispose'));
ok(load(NS, 'symbol/metadata'));
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Promise from 'core-js-pure/full/promise';
import Promise from 'core-js-pure/es/promise';

QUnit.test('Promise.try', assert => {
assert.isFunction(Promise.try);
Expand Down

0 comments on commit 71d37cb

Please sign in to comment.