The
'rejectionHandled'
event is emitted whenever a
Promise
has been rejected
and an error handler was attached to it (using
promise.catch()
, for
example) later than one turn of the Node.js event loop.
The
Promise
object would have previously been emitted in an
'unhandledRejection'
event, but during the course of processing gained a
rejection handler.
There is no notion of a top level for a
Promise
chain at which rejections can
always be handled. Being inherently asynchronous in nature, a
Promise
rejection can be handled at a future point in time, possibly much later than
the event loop turn it takes for the
'unhandledRejection'
event to be emitted.
Another way of stating this is that, unlike in synchronous code where there is
an ever-growing list of unhandled exceptions, with Promises there can be a
growing-and-shrinking list of unhandled rejections.
In synchronous code, the
'uncaughtException'
event is emitted when the list of
unhandled exceptions grows.
In asynchronous code, the
'unhandledRejection'
event is emitted when the list
of unhandled rejections grows, and the
'rejectionHandled'
event is emitted
when the list of unhandled rejections shrinks.
In this example, the
unhandledRejections
Map
will grow and shrink over time,
reflecting rejections that start unhandled and then become handled. It is
possible to record such errors in an error log, either periodically (which is
likely best for long-running application) or upon process exit (which is likely
most convenient for scripts).