Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shqld/be21d1e82cec4dccfc933477f9b60356 to your computer and use it in GitHub Desktop.
Save shqld/be21d1e82cec4dccfc933477f9b60356 to your computer and use it in GitHub Desktop.
Promises are not correctly handled on Node.js unlike Chrome
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')
}
@shqld
Copy link
Author

shqld commented Jan 5, 2022

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 by Promise.allSettled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment