Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ES2024 Promise.withResolvers #1452

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/InternalBytecode/01-Promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,17 @@
return error;
}

function withResolvers() {
var resolve, reject;
var promise = new this(function(doResolve, doReject) {
resolve = doResolve;
reject = doReject;
});
return { resolve, reject, promise };
}

core.withResolvers = withResolvers;

core.any = function promiseAny(values) {
return new core(function(resolve, reject) {
var promises = iterableToArray(values);
Expand Down
40 changes: 40 additions & 0 deletions test/hermes/promise-withResolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: %hermes -Xes6-promise %s | %FileCheck --match-full-lines %s
// RUN: %hermes -Xmicrotask-queue %s | %FileCheck --match-full-lines %s
// RUN: %hermesc -O -emit-binary -out %t.hbc %s && %hermes -Xes6-promise %t.hbc | %FileCheck --match-full-lines %s

print('Promise.withResolvers');
// CHECK-LABEL: Promise.withResolvers

function PromiseLike(executor) {
executor(
function (v) { print('Resolved:', v) },
function (e) { print('Rejected:', e) }
);
}

var {resolve} = Promise.withResolvers.call(PromiseLike)
resolve('message1')
// CHECK-NEXT: Resolved: message1

var {resolve, promise} = Promise.withResolvers();
resolve('success withResolver!');
promise.then(function (message) {
print('Resolved:', message);
});
// CHECK-NEXT: Resolved: success withResolver!

var {reject, promise} = Promise.withResolvers();
reject('failure withResolver!');
promise.then(function (message) {
print('Resolved:', message);
}).catch(function(e) {
print('Rejection:', e)
});
// CHECK-NEXT: Rejection: failure withResolver!
4 changes: 4 additions & 0 deletions test/hermes/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ print('allSettled' in Promise);
print('any' in Promise);
// CHECK-NEXT: true

print('withResolvers' in Promise);
// CHECK-NEXT: true


var promise = new Promise(function(res, rej) {
res('success!');
});
Expand Down
Loading