diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f64d230aae6..52b04a7d7eb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## Changelog ##### Unreleased +- [`Promise.try`](https://github.com/tc39/proposal-promise-try): + - Built-ins: + - `Promise.try` + - Added optional arguments support, [promise-try/16](https://github.com/tc39/proposal-promise-try/pull/16) + - Moved to stage 2.7, [April 2024 TC39 meeting](https://twitter.com/ljharb/status/1777398184387514544) - Added [`URL.parse`](https://url.spec.whatwg.org/#dom-url-parse), [url/825](https://github.com/whatwg/url/pull/825) - [`RegExp.escape`](https://github.com/tc39/proposal-regex-escaping) [moved to hex-escape semantics](https://github.com/tc39/proposal-regex-escaping/pull/67) - Some minor updates of [Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management) Stage 3 proposal like [explicit-resource-management/217](https://github.com/tc39/proposal-explicit-resource-management/pull/217) diff --git a/README.md b/README.md index 5cbb11a90793..c4ec3f2701bb 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3]) - [Explicit resource management](#explicit-resource-management) - [`Symbol.metadata` for decorators metadata proposal](#symbolmetadata-for-decorators-metadata-proposal) - [Stage 2.7 proposals](#stage-27-proposals) + - [`Promise.try`](#promisetry) - [Stage 2 proposals](#stage-2-proposals) - [`AsyncIterator` helpers](#asynciterator-helpers) - [`Iterator.range`](#iteratorrange) @@ -170,7 +171,6 @@ structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3]) - [`Array.isTemplateObject`](#arrayistemplateobject) - [`String.dedent`](#stringdedent) - [`RegExp` escaping](#regexp-escaping) - - [`Promise.try`](#promisetry) - [`Symbol` predicates](#symbol-predicates) - [`Uint8Array` to / from base64 and hex](#uint8array-to-from-base64-and-hex) - [Stage 1 proposals](#stage-1-proposals) @@ -2498,7 +2498,28 @@ core-js(-pure)/actual|full/function/metadata core-js(-pure)/stage/2.7 ``` -*Nothing* +##### [`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) +```js +class Promise { + static try(callbackfn: Function): Promise; +} +``` +[*CommonJS entry points:*](#commonjs-api) +```js +core-js/proposals/promise-try +core-js(-pure)/full/promise/try +``` +[*Examples*](https://goo.gl/k5GGRo): +```js +Promise.try(() => 42).then(it => console.log(`Promise, resolved as ${it}`)); + +Promise.try(() => { throw 42; }).catch(it => console.log(`Promise, rejected as ${it}`)); + +Promise.try(async () => 42).then(it => console.log(`Promise, resolved as ${it}`)); + +Promise.try(async () => { throw 42; }).catch(it => console.log(`Promise, rejected as ${it}`)); +``` #### Stage 2 proposals[⬆](#index) [*CommonJS entry points:*](#commonjs-api) @@ -2692,29 +2713,6 @@ console.log(RegExp.escape('\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u200 // => '\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\xa0\\u1680\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000\\u2028\\u2029\\ufeff' ``` -##### [`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) -```js -class Promise { - static try(callbackfn: Function): Promise; -} -``` -[*CommonJS entry points:*](#commonjs-api) -```js -core-js/proposals/promise-try -core-js(-pure)/full/promise/try -``` -[*Examples*](https://goo.gl/k5GGRo): -```js -Promise.try(() => 42).then(it => console.log(`Promise, resolved as ${it}`)); - -Promise.try(() => { throw 42; }).catch(it => console.log(`Promise, rejected as ${it}`)); - -Promise.try(async () => 42).then(it => console.log(`Promise, resolved as ${it}`)); - -Promise.try(async () => { throw 42; }).catch(it => console.log(`Promise, rejected as ${it}`)); -``` - ##### [`Symbol` predicates](https://github.com/tc39/proposal-symbol-predicates)[⬆](#index) Modules [`esnext.symbol.is-registered-symbol`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.symbol.is-registered-symbol.js), [`esnext.symbol.is-well-known-symbol`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.symbol.is-well-known-symbol.js). ```js diff --git a/packages/core-js/modules/esnext.promise.try.js b/packages/core-js/modules/esnext.promise.try.js index 9952fb665367..05d4b2528744 100644 --- a/packages/core-js/modules/esnext.promise.try.js +++ b/packages/core-js/modules/esnext.promise.try.js @@ -1,14 +1,20 @@ 'use strict'; var $ = require('../internals/export'); +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'); // `Promise.try` method // https://github.com/tc39/proposal-promise-try $({ target: 'Promise', stat: true, forced: true }, { - 'try': function (callbackfn) { + 'try': function (callbackfn /* , ...args */) { + var args = slice(arguments, 1); var promiseCapability = newPromiseCapabilityModule.f(this); - var result = perform(callbackfn); + var result = perform(function () { + return apply(aCallable(callbackfn), undefined, args); + }); (result.error ? promiseCapability.reject : promiseCapability.resolve)(result.value); return promiseCapability.promise; } diff --git a/packages/core-js/stage/2.7.js b/packages/core-js/stage/2.7.js index fd28e3f4b7e1..eb63b47a92ae 100644 --- a/packages/core-js/stage/2.7.js +++ b/packages/core-js/stage/2.7.js @@ -1,6 +1,6 @@ 'use strict'; var parent = require('./3'); -// nothing +require('../proposals/promise-try'); module.exports = parent; diff --git a/packages/core-js/stage/2.js b/packages/core-js/stage/2.js index 1beb1ec267a7..7a78ca224f29 100644 --- a/packages/core-js/stage/2.js +++ b/packages/core-js/stage/2.js @@ -5,7 +5,6 @@ require('../proposals/array-is-template-object'); require('../proposals/async-iterator-helpers'); require('../proposals/iterator-range'); require('../proposals/map-upsert-stage-2'); -require('../proposals/promise-try'); require('../proposals/regexp-escaping'); require('../proposals/string-dedent'); require('../proposals/symbol-predicates-v2'); diff --git a/tests/unit-global/esnext.promise.try.js b/tests/unit-global/esnext.promise.try.js index ea6ac4547a8e..51a21e15a50d 100644 --- a/tests/unit-global/esnext.promise.try.js +++ b/tests/unit-global/esnext.promise.try.js @@ -12,6 +12,12 @@ QUnit.test('Promise.try, resolved', assert => { }); }); +QUnit.test('Promise.try, resolved, with args', assert => { + return Promise.try((a, b) => Promise.resolve(a + b), 1, 2).then(it => { + assert.same(it, 3, 'resolved with a correct value'); + }); +}); + QUnit.test('Promise.try, rejected', assert => { return Promise.try(() => { throw new Error(); diff --git a/tests/unit-pure/esnext.promise.try.js b/tests/unit-pure/esnext.promise.try.js index 140844a90dc7..014b081e5c3d 100644 --- a/tests/unit-pure/esnext.promise.try.js +++ b/tests/unit-pure/esnext.promise.try.js @@ -12,6 +12,12 @@ QUnit.test('Promise.try, resolved', assert => { }); }); +QUnit.test('Promise.try, resolved, with args', assert => { + return Promise.try((a, b) => Promise.resolve(a + b), 1, 2).then(it => { + assert.same(it, 3, 'resolved with a correct value'); + }); +}); + QUnit.test('Promise.try, rejected', assert => { return Promise.try(() => { throw new Error();