Created
July 24, 2018 03:16
-
-
Save tomsaleeba/f20e1d1c248aeaf3eaf8c50e24fb70b9 to your computer and use it in GitHub Desktop.
NodeJS async/await error propagation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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