-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
async_hooks: add AsyncResource.bindCurrent() #43033
Conversation
This static method does roughly the same thing as AsyncResource.bind(), except that it does not create a new AsyncResource. Instead, it relies on the existing current one. This is useful in situations where a callback is intended to be run in the _current_ context regardless of when/where it's called.
@nodejs/async_hooks |
added: REPLACEME | ||
--> | ||
|
||
* `fn` {Function} The function to bind to the current execution context. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would one use bindCurrent
instead of bind
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost any time you'd think you'd want to use the static bind
. It avoids the creation of a new AsyncResource
instance, which in many cases isn't actually needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we explain in the documentation when bind
is actually needed?
Another option here would be to make |
Converted this to a draft, because I'd like to explore just modifying |
Closing this in favour of #43065. It turned out this approach (i.e. replacing the the |
This new static method does roughly the same thing as
AsyncResource.bind()
, except that it does not create a newAsyncResource
. Instead, it relies on the existing current one.This is an optimization useful in basically any situation where
bind()
would be called without passing (or relying on) thetype
argument.I opted to make a completely different method, rather than overload
bind()
even more than it already is, but I can change that if desired.