Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NodeJS async/await error propagation
// direct as promise
;(function () {
const prefix = '[direct-promise level]'
async function direct () {
throw new Error(`${prefix} explosion`)
}
direct().then(() => {
console.log(`${prefix} success`)
}).catch(err => {
console.error(`${prefix} failed with error=${err}`) // we hit this case
})
})()
// direct as async/await
;(async function () {
const prefix = '[direct-async level]'
async function direct () {
throw new Error(`${prefix} explosion`)
}
try {
const result = await direct()
console.log(`${prefix} success with result=${result}`)
} catch (err) {
console.error(`${prefix} failed with error=${err}`) // we hit this case
}
})()
// 1 level deep
;(async function () {
const prefix = '[1 level]'
async function level1 () {
async function direct () {
throw new Error(`${prefix} explosion`)
}
await direct()
}
try {
const result = await level1()
console.log(`${prefix} success with result=${result}`)
} catch (err) {
console.error(`${prefix} failed with error=${err}`) // we hit this case
}
})()
// 2 levels deep
;(async function () {
const prefix = '[2 level]'
async function level2 () {
async function level1 () {
async function direct () {
throw new Error(`${prefix} explosion`)
}
await direct()
}
await level1()
}
try {
const result = await level2()
console.log(`${prefix} success with result=${result}`)
} catch (err) {
console.error(`${prefix} failed with error=${err}`) // we hit this case
}
})()
// 3 levels deep
;(async function () {
const prefix = '[3 level]'
async function level3 () {
async function level2 () {
async function level1 () {
async function direct () {
throw new Error(`${prefix} explosion`)
}
await direct()
}
await level1()
}
await level2()
}
try {
const result = await level3()
console.log(`${prefix} success with result=${result}`)
} catch (err) {
console.error(`${prefix} failed with error=${err}`) // we hit this case
}
})()
// inside a .then()
;(async function () {
const prefix = '[in .then()]'
function level2 () {
async function level1 () {
async function direct () {
throw new Error(`${prefix} explosion`)
}
await direct()
}
return level1()
}
Promise.resolve().then(async () => {
return level2()
}).then(result => {
console.log(`${prefix} success with result=${result}`)
}).catch(err => {
console.error(`${prefix} failed with error=${err}`) // we hit this case
})
})()
// inside a callback with try-catch
;(async function () {
const prefix = '[in cb() try-catch]'
async function direct () {
throw new Error(`${prefix} explosion`)
}
function doCallback (cb) {
cb()
}
doCallback(async () => {
try {
await direct()
console.log(`${prefix} success`)
} catch (err) {
console.error(`${prefix} failed with error=${err}`) // we hit this case
}
})
})()
// inside a callback with .catch()
;(async function () {
const prefix = '[in cb() .catch()]'
async function direct () {
throw new Error(`${prefix} explosion`)
}
function doCallback (cb) {
cb()
}
doCallback(async () => {
await direct().catch(err => {
console.error(`${prefix} failed with error=${err}`) // we hit this case
})
console.log(`${prefix} will run after .direct() has returned`)
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment