Skip to content

Is it possible to hook into an asynchronous function? #1030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nabetti1720 opened this issue Apr 19, 2025 · 4 comments · May be fixed by #1033
Open

Is it possible to hook into an asynchronous function? #1030

nabetti1720 opened this issue Apr 19, 2025 · 4 comments · May be fixed by #1033

Comments

@nabetti1720
Copy link

nabetti1720 commented Apr 19, 2025

Hello everyone, I'm part of an experimental project called LLRT. As you know, LLRT uses quickjs-ng through RquickJS (Rust binding). I'm very grateful to be able to use such a great js engine.

Currently, we are trying to implement async_hooks, which is one of the nodejs modules. A draft has been made, but we want to make it better.

The current implementation creates hooking points for Promise, setTimeout, and setImmediate by patching JavaScript, but we would prefer something more native.

Current Implementation :
https://github.com/awslabs/llrt/pull/947/files#diff-801982e5802bc4502f37e9606801a09f83274a59675f658ee91eac80c54925a5

Is there a better way?

@bnoordhuis
Copy link
Contributor

async_hooks in node is implemented using V8's PromiseHook(s)1 API. Assuming you're familiar with it, would that work for you?

1 V8 is not wholly consistent on whether it's singular or plural...

@nabetti1720
Copy link
Author

nabetti1720 commented Apr 20, 2025

I'm not that familiar with asynchronous processing, but I'm hoping that the js engine has something like this, as you say.

Promise lifecycle hooks in V8 (It's a bit of an old document) :
https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit?tab=t.0#heading=h.sdpzjcyw06a5

@saghul
Copy link
Contributor

saghul commented Apr 20, 2025

Adding an API inspired by V8's sounds like a good idea to me!

bnoordhuis added a commit to bnoordhuis/quickjs that referenced this issue Apr 21, 2025
Lets embedders track promise chains. Closely modelled after
the V8 API of the same name.

Fixes: quickjs-ng#1030
@bnoordhuis bnoordhuis linked a pull request Apr 21, 2025 that will close this issue
@bnoordhuis
Copy link
Contributor

@nabetti1720 can you check if #1033 works for you?

bnoordhuis added a commit to bnoordhuis/quickjs that referenced this issue Apr 22, 2025
Lets embedders track promise chains. Closely modelled after
the V8 API of the same name.

Fixes: quickjs-ng#1030
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants