Skip to content

Instantly share code, notes, and snippets.

@Aschen
Created October 3, 2023 09:43
Show Gist options
  • Save Aschen/79e55f495495ca953325c0629db778ef to your computer and use it in GitHub Desktop.
Save Aschen/79e55f495495ca953325c0629db778ef to your computer and use it in GitHub Desktop.
Return await vs return promise

Result:

Error: whoops
    at last (/Users/adrien/projects/didask/odc/test.ts:43:9)
    at middle (/Users/adrien/projects/didask/odc/test.ts:47:16)
    at first (/Users/adrien/projects/didask/odc/test.ts:51:16)
    at run (/Users/adrien/projects/didask/odc/test.ts:56:11)
    at Object.<anonymous> (/Users/adrien/projects/didask/odc/test.ts:68:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
Error: whoops without await
    at lastBis (/Users/adrien/projects/didask/odc/test.ts:31:9)
    at middleBis (/Users/adrien/projects/didask/odc/test.ts:35:10)
    at firstBis (/Users/adrien/projects/didask/odc/test.ts:39:10)
    at run (/Users/adrien/projects/didask/odc/test.ts:62:11)
    
async function lastBis() {
throw new Error('whoops without await')
}
async function middleBis() {
return lastBis()
}
async function firstBis() {
return middleBis()
}
async function last() {
throw new Error('whoops')
}
async function middle() {
return await last()
}
async function first() {
return await middle()
}
async function run() {
try {
await first()
} catch (error) {
console.error(error)
}
try {
await firstBis()
} catch (error) {
console.error(error)
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment