-
Notifications
You must be signed in to change notification settings - Fork 30k
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
AsyncResource.bind does not forward arguments #36051
Comments
@lroal, can you please edit your issue to include the code that does not work and describe what you expect to happen? You can also include the workaround, but what is the most important is the code that does not work. |
Here is the example, @mmomtchev . let {AsyncResource} = require('async_hooks');
let cb = AsyncResource.bind(onData);
cb('foo');
function onData(arg) {
console.log(arg); //undefined
} |
The first argument passed to the bound function is let {AsyncResource} = require('async_hooks');
function onData(arg) {
console.log(this, arg); // foo bar
}
let cb = AsyncResource.bind(onData);
cb('foo', 'bar'); |
Then that's a documentation issue |
It prints: [String: 'foo'] bar The behaviour is very surprising. Why do we need to involve the "this" key word ? |
The API is just a wrapper around asyncResource.runInAsyncScope which allows to pass this similar as But it seems reusing the name |
Definitely a documentation issue. So, the correct use of the function is: const ar = new AsyncResource('foo')
const fn = ar.bind((...args) => console.log(this, args));
fn({}, 1, 2, 3); // where the {} is thisArg We could modify the API to accept the additional const ar = new AsyncResource('foo')
const fn = ar.bind((...args) => console.log(this, args), {}); // pass the additional this arg
fn(1, 2, 3); Would make for a good first contribution to do both the doc update (to add an example of correct usage) and add the additional argument. |
Semver-major Support setting the `thisArg` for AsyncResource.bind and AsyncResource.prototype.bind. If `thisArg` is not set, then `this` will be set to the `AsyncResource` instance. Fixes: nodejs#36051 Signed-off-by: James M Snell <jasnell@gmail.com>
Using Node 12.19.0:
When binding a function with AsyncResource.bind, the arguments are not forwarded.
I had to write this workaround to make it work:
The text was updated successfully, but these errors were encountered: