[Question]: Is possible to get parent promise from PromiseHookInit through async_hooks #13302
Comments
|
Ping @trevnorris @addaleax |
JiaLiPassion
commented
May 30, 2017
•
|
@jasnell , thank you ! I need this Also ping @AndreasMadsen, @matthewloring ! from the PromiseHook Sample https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit# there is code like this. const async_wrap = process.binding('async_wrap');
const chain = new Map();
const startTimes = new Map();
const endTimes = new Map();
let root;
function init(id, parentId) {
if (parentId !== undefined) {
chain.set(parentId, id);
} else {
root = id;
}
}
function before(id) { startTimes.set(id, Date.now()); }
function after(id) { endTimes.set(id, Date.now()); }
async_wrap.setupHooks({init, before, after});
async_wrap.enable();
doSomeAsyncOp()
.then(() => { .. })
.then(() => { .. })
async_wrap.disable();
let totalTime = 0;
while(root = chain.get(root)) {
totalTime += (endTimes.get(root) - startTimes.get(root));
}
console.log(totalTime);
const p = new Promise ( resolve => resolve(1) );
const p1 = p.then(val => { console.log(val == 1); return 2; });
const p2 = p.then(val => console.log(val == 1));
the call order will like
This is the expected result, but current nodejs 8 nightly build, the code is const async_hooks = require('async_hooks');
function init(id, provider, parentId, parentHandle) {
process._rawDebug('init ', id, provider, parentId, parentHandle);
}
function before(id) {
process._rawDebug('before', id);
}
function after(id) {
process._rawDebug('after', id);
}
function destroy(id) {
process._rawDebug('destroy', id);
}
async_hooks.createHook({init, before, after, destroy}).enable();
const p = new Promise ( resolve => {
process._rawDebug('execute p');
resolve(1);} );
const p1 = p.then(val => { process._rawDebug('p1 then', val); return 2; });
const p2 = p1.then(val => process._rawDebug('p2', val));
and the output is
The parentId are all edit (AndreasMadsen): added syntax highlighting |
AndreasMadsen
added async_hooks question
labels
May 30, 2017
mscdex
added the
promises
label
May 30, 2017
|
I need confirmation that I understand the question/feature request correctly. Consider this code: const async_hooks = require('async_hooks');
async_hooks.createHook({
init(id, type, triggerId, resource) {
process._rawDebug('init', {id, type, triggerId, resource});
},
before(id) {
process._rawDebug('before', {id});
},
after(id) {
process._rawDebug('after', {id});
},
destroy(id) {
process._rawDebug('destroy', {id});
}
}).enable();
const p2 = new Promise(function (resolve) {
resolve(1);
});
const p3 = p2.then(function (val) {
return 2;
});This results in the following output:
You would like the I think that is fair, it shouldn't be difficult implement. Honestly, the trigger id stuff is a little confusing to me, I would like to see a clear line when we set the |
JiaLiPassion
commented
May 30, 2017
•
|
@AndreasMadsen , thank you for the reply.
Yes, that will help. And If the implementation become like this, I have another question is
Can I say that |
Not always, If we implement as suggested, you can check if |
JiaLiPassion commentedMay 30, 2017
Version:
v8.0.0-nightly20170527f84666f923
Platform:
Subsystem:
Chrome V8 PromiseHook
inithook's signature isPromiseHookInit(promise,parentPromise),current
async_hooksseems not support to get theparentPromise, is that possible to get thethe parentPromise from
async_hooks initcallback?Thank you very much!