Created
May 17, 2016 17:10
-
-
Save subfuzion/b33498e88902015de9cc0e4e9dc1d247 to your computer and use it in GitHub Desktop.
Await with error
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
/* eslint-disable no-constant-condition, no-console, babel/no-await-in-loop */ | |
import 'babel-polyfill' | |
import 'source-map-support/register' | |
function echo(i, badModulus = 3) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (i % badModulus === 0) { | |
return reject(new Error('boom')) | |
} | |
resolve(i) | |
}, 1000) | |
}) | |
} | |
async function listen() { | |
let i = 0 | |
while (true) { | |
i++ | |
try { | |
echo(i, 2).catch(err => { | |
console.log('kaboom') | |
}) | |
let m = await echo(i) | |
console.log(m) | |
} catch (err) { | |
console.log(`caught error: ${err.message}`) | |
//throw err | |
} | |
} | |
} | |
process.on('uncaughtException', (err) => { | |
console.log(`Caught exception: ${err}`); | |
process.exit(1) | |
}); | |
process.on('unhandledRejection', (reason, p) => { | |
console.log(`Unhandled rejection. reason: ${reason}`); | |
process.exit(1) | |
}); | |
if (require.main === module) { | |
listen().catch(err => { | |
console.log(`top level: ${err.message}`) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment