-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
4 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |