Skip to content

Instantly share code, notes, and snippets.

@Conduitry
Created September 5, 2016 22:31
Show Gist options
  • Save Conduitry/845982d0b3f1de319f3ad5a0cdbc9433 to your computer and use it in GitHub Desktop.
Save Conduitry/845982d0b3f1de319f3ad5a0cdbc9433 to your computer and use it in GitHub Desktop.
Display warning messages in Node.js apps when rejected promises are not handled
let rejectionTimeouts = new WeakMap()
function displayError({ stack }) {
process.stderr.write(stack + '\n')
}
process.on('unhandledRejection', (err, promise) => {
rejectionTimeouts.set(promise, setTimeout(() => displayError(err), 200))
})
process.on('rejectionHandled', promise => {
clearTimeout(rejectionTimeouts.get(promise))
rejectionTimeouts.delete(promise)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment