Detecting Unhandled Rejections with Node.js
Learn how Node.js detects and reports unhandled rejections.
We'll cover the following
Tracking unhandled rejections in Node.js
Node.js tracks unhandled promise rejections in a similar, but not identical, way that browsers do. There are two events in Node.js, but these are emitted on the process
object and have different capitalizations than the browser events:
unhandledRejection
: This is emitted when a promise is rejected and no rejection handler is called within one turn of the event loop.rejectionHandled
: This is emitted when a promise is rejected and a rejection handler is called after one turn of the event loop.
Also, unlike the browser events, these events do not receive an event object. The unhandledRejection
event handler is passed both the rejection reason and the promise that was rejected as arguments. The following code shows unhandledRejection
in action:
Get hands-on with 1200+ tech skills courses.