Open this example in the interactive library
Source code
async function work() {
try {
return "result";
} finally {
await Promise.resolve();
console.log("cleanup");
}
}
console.log(await work());
Sample output
cleanup result

Flush asynchronous cleanup before returning in JavaScript.
Open this example in the interactive library
async function work() {
try {
return "result";
} finally {
await Promise.resolve();
console.log("cleanup");
}
}
console.log(await work());
cleanup result