forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://bugs.webkit.org/show_bug.cgi?id=269775 Reviewed by Justin Michaud. This patch implements https://github.com/tc39/proposal-promise-try behind a runtime option; the proposal is currently at Stage 2 but the spec is very simple and not expected to change. Effectively, Promise.try(f) is a more convenient way to write `new Promise((resolve) => { resolve(f()); })` -- we don't care whether f is sync or async, but if is synchronous, we want it to be executed immediately. This implementation includes tc39/proposal-promise-try#16, which the champion expects to merge; it simply extends the API to Promise.try(f, ...args) such that arguments can be forwarded to the callback. * JSTests/stress/promise-try.js: Added. * Source/JavaScriptCore/builtins/PromiseConstructor.js: (try): Added. * Source/JavaScriptCore/runtime/JSPromiseConstructor.cpp: (JSC::JSPromiseConstructor::finishCreation): * Source/JavaScriptCore/runtime/OptionsList.h: Canonical link: https://commits.webkit.org/275081@main
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//@ requireOptions("--usePromiseTryMethod=1") | ||
|
||
function shouldBe(actual, expected) { | ||
if (actual !== expected) | ||
throw new Error(`expected ${expected} but got ${actual}`); | ||
} | ||
|
||
function notReached() { | ||
throw new Error('should not reach here'); | ||
} | ||
|
||
async function delay() { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(resolve); | ||
}); | ||
} | ||
|
||
shouldBe(Promise.try.length, 1); | ||
|
||
async function test() { | ||
{ | ||
let result = []; | ||
Promise.try(() => { result.push(1); }); | ||
result.push(2); | ||
await delay(); | ||
shouldBe(`${result}`, '1,2'); | ||
} | ||
|
||
shouldBe(await Promise.try(() => Promise.resolve(3)), 3); | ||
|
||
try { | ||
await Promise.try(() => { throw 4; }); | ||
notReached(); | ||
} catch (e) { | ||
shouldBe(e, 4); | ||
} | ||
|
||
try { | ||
await Promise.try(() => Promise.reject(5)); | ||
notReached(); | ||
} catch (e) { | ||
shouldBe(e, 5); | ||
} | ||
|
||
shouldBe(await Promise.try((x, y, z) => x + y + z, 1, 2, 3), 6); | ||
} | ||
|
||
test().catch((error) => { | ||
print(`FAIL: ${error}\n${error.stack}`); | ||
$vm.abort(); | ||
}); | ||
drainMicrotasks(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters