Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Created May 17, 2016 17:10
Show Gist options
  • Save subfuzion/b33498e88902015de9cc0e4e9dc1d247 to your computer and use it in GitHub Desktop.
Save subfuzion/b33498e88902015de9cc0e4e9dc1d247 to your computer and use it in GitHub Desktop.
Await with error
/* 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