Last active
January 7, 2022 03:01
-
-
Save shqld/be21d1e82cec4dccfc933477f9b60356 to your computer and use it in GitHub Desktop.
Promises are not correctly handled on Node.js unlike Chrome
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
main() | |
async function main() { | |
const tasks = [] | |
tasks.push(fail()) | |
// Commenting out this line makes it work | |
await new Promise((resolve) => setTimeout(resolve, 0)) | |
tasks.push(fail()) | |
await Promise.allSettled(tasks) | |
console.log('Successfully handled') | |
} | |
async function fail() { | |
throw new Error('failed') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turned out this correctly works as expected. Unlike browsers, Node.js / deno throw an UnhandledRejection while running
await new Promise((resolve) => setTimeout(resolve, 0))
. Thus as a result, tasks can't be awaited for byPromise.allSettled
.