Skip to content

Commit

Permalink
add optional arguments support to Promise.try
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 8, 2024
1 parent 432b130 commit 2d9f3bd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [`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
- 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)
Expand Down
10 changes: 8 additions & 2 deletions packages/core-js/modules/esnext.promise.try.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/unit-global/esnext.promise.try.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions tests/unit-pure/esnext.promise.try.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2d9f3bd

Please sign in to comment.