I do not want to type .catch for every promise I use. Without doing this errors caused by promises are supremely unhelpful.
Using an entire library like bluebird purely for this purpose makes me uncomfortable.
Add a handler to your process for unhandled rejections. You can handle them once like that instead of copy-pasting . catch statements all the time. catch() should be used to handle errors, not just report them.
Promises co-mingle rejected promises and unintended runtime exceptions. Apart from how the API is structured - promises have another major flaw: they treat unintentional native runtime exceptions and intentional rejected promises - which are two drastically different intentions - in the same "path".
When using Javascript promises, does the event loop get blocked? No. Promises are only an event notification system. They aren't an operation themselves.
A Promise is in one of these states: pending: initial state, neither fulfilled nor rejected. fulfilled: meaning that the operation was completed successfully. rejected: meaning that the operation failed.
For error tracking during development, V8 (recent Node.js and Chrome versions) already has got unhandledRejection
(Node.js) and unhandledrejection
(Chrome) event listener by default, which results in UnhandledPromiseRejectionWarning
warning in Node.js and Uncaught (in promise)
error in Chrome.
Deprecation warning in Node 7 states that this will be changed in future Node.js versions:
Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Error handling in promises is not optional and should be treated on par with try...catch
. Every promise that has a chance to be rejected should be chained with catch
. And in the case of async...await
or co
, .catch
is identical to try...catch
.
If the error is supposed to be ignored, it has to be explicitly caught. If error handling needs to be consistent across the app and comply to log levels, this may be provided by design. E.g. with conventional debug
package:
const debug = require('debug');
function catchFn(err, ns = 'myapp:caughtPromises') {
debug(ns)(err);
}
function catchPromise(promise, ns) {
promise.catch(err => catchFn(err, ns));
return promise;
}
...
try {
await promise;
} catch (err) {
catchFn(err);
}
// or
catchPromise(promise);
// or
promise.catch(catchFn);
catchFn
can be extended to use third-party logging service as well.
Add a handler to your process for unhandled rejections. You can handle them once like that instead of copy-pasting .catch statements all the time.
process.on( 'unhandledRejection', ( error, promise ) => {
console.log( 'UPR: ' + promise + ' with ' + error )
console.log( error.stack )
} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With