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