Skip to content

Commit 789d45d

Browse files
authored
Optimise handleAsync for JSPI (#20236)
* Optimise handleAsync for JSPI With current handling, in JSPI mode handleAsync was doing unnecessary double wrapping-unwrapping of promises: invoke startAsync, it returns the promise, convert it to callback-style function with wakeUp, pass to handleSleep, handleSleep takes the callback and converts it back to another promise. For JSPI Promises are native, so it makes more sense to invert the relationsheep between those 2 APIs and make handleAsync just await the original promise, while handleSleep would be the only one converting between callback and promise styles. * Avoid unsupported syntax for async fn
1 parent a7427d8 commit 789d45d

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/library_async.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -441,21 +441,18 @@ addToLibrary({
441441
isAsyncExport(func) {
442442
return Asyncify.asyncExports && Asyncify.asyncExports.has(func);
443443
},
444-
handleSleep(startAsync) {
444+
handleAsync: async (startAsync) => {
445445
{{{ runtimeKeepalivePush(); }}}
446-
var promise = new Promise((resolve) => {
447-
startAsync(resolve);
448-
});
449-
promise.finally(() => {
446+
try {
447+
return await startAsync();
448+
} finally {
450449
{{{ runtimeKeepalivePop(); }}}
451-
});
452-
return promise;
450+
}
453451
},
454-
handleAsync(startAsync) {
455-
return Asyncify.handleSleep((wakeUp) => {
456-
// TODO: add error handling as a second param when handleSleep implements it.
457-
startAsync().then(wakeUp);
458-
});
452+
handleSleep(startAsync) {
453+
return Asyncify.handleAsync(() => (
454+
new Promise((wakeUp) => startAsync(wakeUp))
455+
));
459456
},
460457
makeAsyncFunction(original) {
461458
#if ASYNCIFY_DEBUG

0 commit comments

Comments
 (0)