Created
September 5, 2016 22:31
-
-
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
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
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