Skip to content

Commit 06ee782

Browse files
benjamingrrvagg
authored andcommitted
doc: document 'unhandledRejection' and 'rejectionHandled'
Documents the new unhandled rejection detection API. Documents the new unhandledRejection/rejectionHandled events in the process docuemntation. As agreed on in this issue: #256 (comment) PR-URL: #946 Reviewed-By: Domenic Denicola <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent b65dade commit 06ee782

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

doc/api/process.markdown

+62
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,68 @@ Nine out of ten times nothing happens - but the 10th time, your system is bust.
116116

117117
You have been warned.
118118

119+
## Event: 'unhandledRejection'
120+
121+
Emitted whenever a `Promise` is rejected and no error handler is attached to
122+
the promise within a turn of the event loop. When programming with promises
123+
exceptions are encapsulated as rejected promises. Such promises can be caught
124+
and handled using `promise.catch(...)` and rejections are propagated through
125+
a promise chain. This event is useful for detecting and keeping track of
126+
promises that were rejected whose rejections were not handled yet. This event
127+
is emitted with the following arguments:
128+
129+
- `reason` the object with which the promise was rejected (usually an `Error`
130+
instance).
131+
- `p` the promise that was rejected.
132+
133+
Here is an example that logs every unhandled rejection to the console
134+
135+
process.on('unhandledRejection', function(reason, p) {
136+
console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason);
137+
// application specific logging, throwing an error, or other logic here
138+
});
139+
140+
For example, here is a rejection that will trigger the `'unhandledRejection'`
141+
event:
142+
143+
somePromise.then(function(res) {
144+
return reportToUser(JSON.pasre(res)); // note the typo
145+
}); // no `.catch` or `.then`
146+
147+
## Event: 'rejectionHandled'
148+
149+
Emitted whenever a Promise was rejected and an error handler was attached to it
150+
(for example with `.catch()`) later than after an event loop turn. This event
151+
is emitted with the following arguments:
152+
153+
- `p` the promise that was previously emitted in an 'unhandledRejection'
154+
event, but which has now gained a rejection handler.
155+
156+
There is no notion of a top level for a promise chain at which rejections can
157+
always be handled. Being inherently asynchronous in nature, a promise rejection
158+
can be be handled at a future point in time — possibly much later than the
159+
event loop turn it takes for the 'unhandledRejection' event to be emitted.
160+
161+
Another way of stating this is that, unlike in synchronous code where there is
162+
an ever-growing list of unhandled exceptions, with promises there is a
163+
growing-and-shrinking list of unhandled rejections. In synchronous code, the
164+
'uncaughtException' event tells you when the list of unhandled exceptions
165+
grows. And in asynchronous code, the 'unhandledRejection' event tells you
166+
when the list of unhandled rejections grows, while the 'rejectionHandled'
167+
event tells you when the list of unhandled rejections shrinks.
168+
169+
For example using the rejection detection hooks in order to keep a list of all
170+
the rejected promises at a given time:
171+
172+
var unhandledRejections = [];
173+
process.on('unhandledRejection', function(reason, p) {
174+
unhandledRejections.push(p);
175+
});
176+
process.on('rejectionHandled', function(p) {
177+
var index = unhandledRejections.indexOf(p);
178+
unhandledRejections.splice(index, 1);
179+
});
180+
119181
## Signal Events
120182

121183
<!--type=event-->

0 commit comments

Comments
 (0)