summaryrefslogtreecommitdiff
path: root/lib/internal/async_hooks.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/async_hooks.js')
-rw-r--r--lib/internal/async_hooks.js17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js
index 64f5cb2462..bf803885ca 100644
--- a/lib/internal/async_hooks.js
+++ b/lib/internal/async_hooks.js
@@ -69,6 +69,7 @@ const active_hooks = {
};
const { registerDestroyHook } = async_wrap;
+const { enqueueMicrotask } = internalBinding('task_queue');
// Each constant tracks how many callbacks there are for any given step of
// async execution. These are tracked so if the user didn't include callbacks
@@ -231,14 +232,26 @@ function restoreActiveHooks() {
}
+let wantPromiseHook = false;
function enableHooks() {
- enablePromiseHook();
async_hook_fields[kCheck] += 1;
+
+ wantPromiseHook = true;
+ enablePromiseHook();
}
function disableHooks() {
- disablePromiseHook();
async_hook_fields[kCheck] -= 1;
+
+ wantPromiseHook = false;
+ // Delay the call to `disablePromiseHook()` because we might currently be
+ // between the `before` and `after` calls of a Promise.
+ enqueueMicrotask(disablePromiseHookIfNecessary);
+}
+
+function disablePromiseHookIfNecessary() {
+ if (!wantPromiseHook)
+ disablePromiseHook();
}
// Internal Embedder API //