Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
async_hooks: use resource objects for Promises
Use `PromiseWrap` resource objects whose lifetimes are tied to the `Promise` instances themselves to track promises, and have a `.promise` getter that points to the `Promise` and a `.parent` property that points to the parent Promise’s resource object, if there is any. The properties are implemented as getters for internal fields rather than normal properties in the hope that it helps keep performance for the common case that async_hooks users will often not inspect them. PR-URL: #13452 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
- Loading branch information
Showing
with
111 additions
and 10 deletions.
- +11 −2 doc/api/async_hooks.md
- +76 −8 src/async-wrap.cc
- +1 −0 src/env.h
- +23 −0 test/parallel/test-async-hooks-promise.js
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
const initCalls = []; | ||
|
||
async_hooks.createHook({ | ||
init: common.mustCall((id, type, triggerId, resource) => { | ||
assert.strictEqual(type, 'PROMISE'); | ||
initCalls.push({id, triggerId, resource}); | ||
}, 2) | ||
}).enable(); | ||
|
||
const a = Promise.resolve(42); | ||
const b = a.then(common.mustCall()); | ||
|
||
assert.strictEqual(initCalls[0].triggerId, 1); | ||
assert.strictEqual(initCalls[0].resource.parentId, undefined); | ||
assert.strictEqual(initCalls[0].resource.promise, a); | ||
assert.strictEqual(initCalls[1].triggerId, initCalls[0].id); | ||
assert.strictEqual(initCalls[1].resource.parentId, initCalls[0].id); | ||
assert.strictEqual(initCalls[1].resource.promise, b); |