@@ -117,7 +117,8 @@ function unhandledRejection(promise, reason) {
117117 maybeUnhandledPromises . set ( promise , {
118118 reason,
119119 uid : ++ lastPromiseId ,
120- warned : false
120+ warned : false ,
121+ domain : process . domain
121122 } ) ;
122123 // This causes the promise to be referenced at least for one tick.
123124 pendingUnhandledRejections . push ( promise ) ;
@@ -192,26 +193,32 @@ function processPromiseRejections() {
192193 }
193194 promiseInfo . warned = true ;
194195 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+ }
195202 switch ( unhandledRejectionsMode ) {
196203 case kStrictUnhandledRejections : {
197204 const err = reason instanceof Error ?
198205 reason : generateUnhandledRejectionError ( reason ) ;
199206 triggerUncaughtException ( err , true /* fromPromise */ ) ;
200- const handled = process . emit ( 'unhandledRejection' , reason , promise ) ;
207+ const handled = emit ( reason , promise , promiseInfo ) ;
201208 if ( ! handled ) emitUnhandledRejectionWarning ( uid , reason ) ;
202209 break ;
203210 }
204211 case kIgnoreUnhandledRejections : {
205- process . emit ( 'unhandledRejection' , reason , promise ) ;
212+ emit ( reason , promise , promiseInfo ) ;
206213 break ;
207214 }
208215 case kAlwaysWarnUnhandledRejections : {
209- process . emit ( 'unhandledRejection' , reason , promise ) ;
216+ emit ( reason , promise , promiseInfo ) ;
210217 emitUnhandledRejectionWarning ( uid , reason ) ;
211218 break ;
212219 }
213220 case kThrowUnhandledRejections : {
214- const handled = process . emit ( 'unhandledRejection' , reason , promise ) ;
221+ const handled = emit ( reason , promise , promiseInfo ) ;
215222 if ( ! handled ) {
216223 const err = reason instanceof Error ?
217224 reason : generateUnhandledRejectionError ( reason ) ;
@@ -220,7 +227,7 @@ function processPromiseRejections() {
220227 break ;
221228 }
222229 case kWarnWithErrorCodeUnhandledRejections : {
223- const handled = process . emit ( 'unhandledRejection' , reason , promise ) ;
230+ const handled = emit ( reason , promise , promiseInfo ) ;
224231 if ( ! handled ) {
225232 emitUnhandledRejectionWarning ( uid , reason ) ;
226233 process . exitCode = 1 ;
@@ -266,10 +273,9 @@ function generateUnhandledRejectionError(reason) {
266273function listenForRejections ( ) {
267274 setPromiseRejectCallback ( promiseRejectHandler ) ;
268275}
269-
270276module . exports = {
271277 hasRejectionToWarn,
272278 setHasRejectionToWarn,
273279 listenForRejections,
274- processPromiseRejections
280+ processPromiseRejections,
275281} ;
0 commit comments