Created
April 5, 2018 10:12
-
-
Save rimiti/697827faefe51022b561ab12e802a4ec to your computer and use it in GitHub Desktop.
Node: Await without catching ? It's really bad...
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
const Test = (ms) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if(ms === 20) reject(new Error('Bad value')); | |
resolve('ok'); | |
}, ms); | |
}); | |
} | |
async function testUnhandled() { | |
const res1 = await Test(10); | |
const res2 = await Test(20); | |
console.log('Test done.'); | |
} | |
async function testHandledByTryCatch() { | |
try{ | |
const res1 = await Test(10); | |
const res2 = await Test(20); | |
console.log('Test done.'); | |
} catch (e) { | |
console.log('Error handled (global): ', e); | |
} | |
} | |
async function testHandledByCatch() { | |
const res1 = await Test(10).catch((e) => console.log('Error handled (res1): ', e)); | |
const res2 = await Test(20).catch((e) => console.log('Error handled (res2): ', e)); | |
console.log('Test done.'); | |
} | |
testUnhandled(); | |
testHandledByTryCatch(); | |
testHandledByCatch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: