Skip to content

Instantly share code, notes, and snippets.

@jwerle
Created May 3, 2022 12:04
Show Gist options
  • Save jwerle/1bc93e303c3532abe0bdcbbea75e528d to your computer and use it in GitHub Desktop.
Save jwerle/1bc93e303c3532abe0bdcbbea75e528d to your computer and use it in GitHub Desktop.
function throwsWithoutAwait () {
return asyncError('throwsWithoutAwait')
}
async function throwsWithAwait () {
return await asyncError('throwsWithAwait')
}
async function asyncError (message) {
await sleep(250)
throw new Error(message)
}
async function sleep (ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function main () {
try {
await throwsWithoutAwait()
} catch (err) {
console.error(err.stack || err)
}
try {
await throwsWithAwait()
} catch (err) {
console.error(err.stack || err)
}
}
main()
Error: throwsWithoutAwait
at asyncError (/home/werle/tmp/async-throw-without-await.js:11:9)
at async main (/home/werle/tmp/async-throw-without-await.js:20:5)
Error: throwsWithAwait
at asyncError (/home/werle/tmp/async-throw-without-await.js:11:9)
at async throwsWithAwait (/home/werle/tmp/async-throw-without-await.js:6:10)
at async main (/home/werle/tmp/async-throw-without-await.js:26:5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment