Skip to content

Instantly share code, notes, and snippets.

@tomsaleeba
Created July 24, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomsaleeba/f20e1d1c248aeaf3eaf8c50e24fb70b9 to your computer and use it in GitHub Desktop.
Save tomsaleeba/f20e1d1c248aeaf3eaf8c50e24fb70b9 to your computer and use it in GitHub Desktop.
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