@@ -117,7 +117,8 @@ function unhandledRejection(promise, reason) {
117
117
maybeUnhandledPromises . set ( promise , {
118
118
reason,
119
119
uid : ++ lastPromiseId ,
120
- warned : false
120
+ warned : false ,
121
+ domain : process . domain
121
122
} ) ;
122
123
// This causes the promise to be referenced at least for one tick.
123
124
pendingUnhandledRejections . push ( promise ) ;
@@ -192,26 +193,32 @@ function processPromiseRejections() {
192
193
}
193
194
promiseInfo . warned = true ;
194
195
const { reason, uid } = promiseInfo ;
196
+ function emit ( reason , promise , promiseInfo ) {
197
+ if ( promiseInfo . domain ) {
198
+ return promiseInfo . domain . emit ( 'error' , reason ) ;
199
+ }
200
+ return process . emit ( 'unhandledRejection' , reason , promise ) ;
201
+ }
195
202
switch ( unhandledRejectionsMode ) {
196
203
case kStrictUnhandledRejections : {
197
204
const err = reason instanceof Error ?
198
205
reason : generateUnhandledRejectionError ( reason ) ;
199
206
triggerUncaughtException ( err , true /* fromPromise */ ) ;
200
- const handled = process . emit ( 'unhandledRejection' , reason , promise ) ;
207
+ const handled = emit ( reason , promise , promiseInfo ) ;
201
208
if ( ! handled ) emitUnhandledRejectionWarning ( uid , reason ) ;
202
209
break ;
203
210
}
204
211
case kIgnoreUnhandledRejections : {
205
- process . emit ( 'unhandledRejection' , reason , promise ) ;
212
+ emit ( reason , promise , promiseInfo ) ;
206
213
break ;
207
214
}
208
215
case kAlwaysWarnUnhandledRejections : {
209
- process . emit ( 'unhandledRejection' , reason , promise ) ;
216
+ emit ( reason , promise , promiseInfo ) ;
210
217
emitUnhandledRejectionWarning ( uid , reason ) ;
211
218
break ;
212
219
}
213
220
case kThrowUnhandledRejections : {
214
- const handled = process . emit ( 'unhandledRejection' , reason , promise ) ;
221
+ const handled = emit ( reason , promise , promiseInfo ) ;
215
222
if ( ! handled ) {
216
223
const err = reason instanceof Error ?
217
224
reason : generateUnhandledRejectionError ( reason ) ;
@@ -220,7 +227,7 @@ function processPromiseRejections() {
220
227
break ;
221
228
}
222
229
case kWarnWithErrorCodeUnhandledRejections : {
223
- const handled = process . emit ( 'unhandledRejection' , reason , promise ) ;
230
+ const handled = emit ( reason , promise , promiseInfo ) ;
224
231
if ( ! handled ) {
225
232
emitUnhandledRejectionWarning ( uid , reason ) ;
226
233
process . exitCode = 1 ;
@@ -266,10 +273,9 @@ function generateUnhandledRejectionError(reason) {
266
273
function listenForRejections ( ) {
267
274
setPromiseRejectCallback ( promiseRejectHandler ) ;
268
275
}
269
-
270
276
module . exports = {
271
277
hasRejectionToWarn,
272
278
setHasRejectionToWarn,
273
279
listenForRejections,
274
- processPromiseRejections
280
+ processPromiseRejections,
275
281
} ;
0 commit comments