Created
July 23, 2017 11:00
-
-
Save lll000111/606ce6ddde79b2202138dcc837bfb5a6 to your computer and use it in GitHub Desktop.
ES 2015 async/await error handling demo (better error stacktraces)
This file contains 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
'use strict'; | |
// ============================================================== | |
// HELPERS | |
// ============================================================== | |
// Needed to create a _really_ asynchronous function. A fake asynchronous function (returning "Promise.reject()" | |
// right away, for example) actually creates a stacktrace. | |
const wait = delay => new Promise(resolve => setTimeout(() => resolve(), delay)); | |
// Some deeply nested (in the actual call hierarchy) asynchronous function that throws an error. | |
const fnError = async () => Promise.reject(new Error('Ooohhhh my god, this went wrong!')); | |
// ============================================================== | |
// TEST: OPTION #1 with await, try/catch, new Error(err) | |
// ============================================================== | |
const fnA1 = async () => { | |
await wait(1); | |
try { | |
await fnA2(); | |
} catch(err) { | |
console.log('Caught an error in fn2 call', new Error(err)); | |
throw err; | |
} | |
}; | |
const fnA2 = async () => { | |
await wait(1); | |
try { | |
await fnError(); | |
} catch(err) { | |
console.log('Caught an error in fnError call', new Error(err)); | |
throw err; | |
} | |
}; | |
// ============================================================== | |
// TEST: OPTION #2 with await, try/catch, WITHOUT new Error(err) | |
// ============================================================== | |
const fnB1 = async () => { | |
await wait(1); | |
try { | |
await fnB2(); | |
} catch(err) { | |
console.log('Caught an error in fn2 call', err); | |
throw err; | |
} | |
}; | |
const fnB2 = async () => { | |
await wait(1); | |
try { | |
await fnError(); | |
} catch(err) { | |
console.log('Caught an error in fnError call', err); | |
throw err; | |
} | |
}; | |
// ============================================================== | |
// TEST: OPTION #3 with await, WITHOUT try/catch | |
// ============================================================== | |
const fnC1 = async () => { | |
await wait(1); | |
const result = await fnC2(); | |
return result; | |
}; | |
const fnC2 = async () => { | |
await wait(1); | |
const result = await fnError(); | |
return result; | |
}; | |
// ============================================================== | |
// TEST: OPTION #4 (simply return the sub-function's promise) | |
// ============================================================== | |
const fnD1 = async () => { | |
await wait(1); | |
return fnD2(); | |
}; | |
const fnD2 = async () => { | |
await wait(1); | |
return fnError(); | |
}; | |
// ============================================================== | |
// RUN DEMOS | |
// ============================================================== | |
(async () => { | |
console.log('\nOPTION #1 (with await, try/catch), new Error(err))\n'); | |
await fnA1().catch(console.error); | |
console.log('\n\nOPTION #2 (with await, try/catch - WITHOUT new Error(err))\n'); | |
await fnB1().catch(console.error); | |
console.log('\n\nOPTION #3 (with await, no try/catch, WITHOUT new Error(err))\n'); | |
await fnC1().catch(console.error); | |
console.log('\n\nOPTION #4 (simply return the sub-function\'s promise)\n'); | |
await fnD1().catch(console.error); | |
})() | |
.catch(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment