Skip to content

Commit

Permalink
fix AsyncResource.bind not using 'this' from the caller by default
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Mar 1, 2022
1 parent ef30a22 commit 141977e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
21 changes: 12 additions & 9 deletions lib/async_hooks.js
Expand Up @@ -223,15 +223,18 @@ class AsyncResource {
return this[trigger_async_id_symbol];
}

bind(fn, thisArg = this) {
bind(fn, thisArg) {
validateFunction(fn, 'fn');
const ret =
FunctionPrototypeBind(
this.runInAsyncScope,
this,
fn,
thisArg);
ObjectDefineProperties(ret, {
const runInAsyncScope = FunctionPrototypeBind(
this.runInAsyncScope,
this,
fn);
const bound = function (...args) {
return runInAsyncScope(
thisArg !== undefined ? thisArg : this,
...args);
};
ObjectDefineProperties(bound, {
'length': {
configurable: true,
enumerable: false,
Expand All @@ -245,7 +248,7 @@ class AsyncResource {
writable: true,
}
});
return ret;
return bound;
}

static bind(fn, type, thisArg) {
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-asyncresource-bind.js
Expand Up @@ -41,11 +41,16 @@ const fn3 = asyncResource.bind(common.mustCall(function() {
fn3();

const fn4 = asyncResource.bind(common.mustCall(function() {
assert.strictEqual(this, asyncResource);
assert.strictEqual(this, undefined);
}));
fn4();

const fn5 = asyncResource.bind(common.mustCall(function() {
assert.strictEqual(this, false);
}), false);
fn5();

const fn6 = asyncResource.bind(common.mustCall(function() {
assert.strictEqual(this, 'test');
}));
fn6.call('test');

0 comments on commit 141977e

Please sign in to comment.