Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sajal50 committed Feb 7, 2021
1 parent 907d6b6 commit 6ed05c9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ module.exports = {
emitBefore: emitBeforeScript,
emitAfter: emitAfterScript,
emitDestroy: emitDestroyScript,
pushAsyncContext,
popAsyncContext,
registerDestroyHook,
useDomainTrampoline,
nativeHooks: {
Expand Down
38 changes: 30 additions & 8 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const {
triggerUncaughtException
} = internalBinding('errors');

const {
pushAsyncContext,
popAsyncContext,
} = require('internal/async_hooks');
const async_hooks = require('async_hooks');

const { emit } = require('process');

// *Must* match Environment::TickInfo::Fields in src/env.h.
const kHasRejectionToWarn = 1;

Expand Down Expand Up @@ -116,12 +124,31 @@ function resolveError(type, promise, reason) {
}

function unhandledRejection(promise, reason) {
const asyncId = async_hooks.executionAsyncId();
const triggerAsyncId = async_hooks.triggerAsyncId();
const resource = promise;

const emit = (reason, promise, promiseInfo) => {
try {
pushAsyncContext(asyncId, triggerAsyncId, resource);
if (promiseInfo.domain) {
return promiseInfo.domain.emit('error', reason);
}
return process.emit('unhandledRejection', reason, promise);
}
finally {
popAsyncContext(asyncId);
}
};

maybeUnhandledPromises.set(promise, {
reason,
uid: ++lastPromiseId,
warned: false,
domain: process.domain
domain: process.domain,
emit
});
console.log("in unhandledRejection", require('async_hooks').executionAsyncId());
// This causes the promise to be referenced at least for one tick.
ArrayPrototypePush(pendingUnhandledRejections, promise);
setHasRejectionToWarn(true);
Expand Down Expand Up @@ -194,13 +221,8 @@ function processPromiseRejections() {
continue;
}
promiseInfo.warned = true;
const { reason, uid } = promiseInfo;
function emit(reason, promise, promiseInfo) {
if (promiseInfo.domain) {
return promiseInfo.domain.emit('error', reason);
}
return process.emit('unhandledRejection', reason, promise);
}
const { reason, uid, emit } = promiseInfo;

switch (unhandledRejectionsMode) {
case kStrictUnhandledRejections: {
const err = reason instanceof Error ?
Expand Down

0 comments on commit 6ed05c9

Please sign in to comment.