| 354 | }; |
| 355 | |
| 356 | function addCatch(that, promise, type, args) { |
| 357 | if (!that[kCapture]) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // Handle Promises/A+ spec, then could be a getter |
| 362 | // that throws on second use. |
| 363 | try { |
| 364 | const then = promise.then; |
| 365 | |
| 366 | if (typeof then === 'function') { |
| 367 | then.call(promise, undefined, function(err) { |
| 368 | // The callback is called with nextTick to avoid a follow-up |
| 369 | // rejection from this promise. |
| 370 | process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args); |
| 371 | }); |
| 372 | } |
| 373 | } catch (err) { |
| 374 | that.emit('error', err); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | function emitUnhandledRejectionOrErr(ee, err, type, args) { |
| 379 | if (typeof ee[kRejection] === 'function') { |