Repository navigation
async-hooks: Is that possible to expose PromiseHooks resolve callback? #14038
Description
Activity
Yes, it would be possible to expose
PromiseHook.resolvein some way, some people have been propising ideas for that. The easiest way would be to “just” add an extra hook just for theresolveevent.Just mentioning one thing that was really confusing me:
const p1 = new Promise(() => {}); // This will never be resolved const p2 = new Promise((resolve) => setTimeout(resolve, 1000, p1)); // resolve p2 with p1 after 1s
p2will still remain a pending promise, but theresolvepromise hook will be called after 1 second, whenresolve()is called. I hope that’s the behaviour you are looking for.- addedasync_hooksIssues and PRs related to the async hooks subsystem.Issues and PRs related to the async hooks subsystem.promisesIssues and PRs related to ECMAScript promises.Issues and PRs related to ECMAScript promises.
on Jul 2, 2017 @addaleax , thank you for reply and point out the case.
const p2 = new Promise((resolve) => setTimeout(resolve, 1000, p1)); // resolve p2 with p1 after 1s
yeah, in this case, if
resolvepromise hook is called, I still don't know whether I shouldschedule a microtask or not.And in my understanding, this is the same with
const p1 = { then() { // do something. } }; const p2 = new Promise((resolve) => setTimeout(resolve, 1000, p1)); // resolve p2 with p1 after 1s
resolve a promise with
thenablewill also leave this promise in pending state, so I realized that I need bothresolvepromise hook and a way to checkpromisestate isresolved,pendingorrejected. I am not sure is that possible to get thoseinternal stateinformation fromv8.so I realized that I need both
resolvepromise hook and a way to checkpromisestate isresolved,pendingorrejected. I am not sure is that possible to get thoseinternal stateinformation fromv8.It is, at least through C++ API and undocumented internal Node APIs (
process.binding('util').getPromiseDetails). I guess if you want to know when a Promise is resolved, you could add a.then()handler, but that’s going to come with problems of its own…Reacted by Isahara86@addaleax ,
process.binding('util').getPromiseDetails, thank you!I guess if you want to know when a Promise is resolved, you could add a .then() handler, but that’s going to come with problems of its own…
yes, if possible , I think in my case
zone.jsis just a wrapper, it should not add.thenfor every promise, so it will be better to resolve the issue withhookapi.@JiaLiPassion As I mentioned, it’s internal, and you should not rely on it; it can be removed or broken at any time.
@addaleax , got it! I will try to find out is there any other way to know
whenthe promise isreallyresolved.
I am still working on porting
zone.jswithasync_hooks. currently when I portingPromise, I met a problem and I think I need theresolvehook to be exposed.case1: promise's parent is an resolved promise.
In this case, current
async_hooksAPI is ok, I can justschedule a microTaskinzone.jsin theinitcallback.case2: the promise's parent is an unresolved promise.
In this case, the
microTaskshould not be scheduled ininit, when thep1.thenis called, becausep1is stillunresolved, themicrotaskshould be scheduled whenp1.resolveiscalled, so I wonder is that possible to expose the
PromiseHook.resolvecallback throughasync_hooks?Thank you very much!