Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69a3792

Browse filesBrowse files
rochdevbengl
authored andcommitted
lib: fix AsyncResource.bind not using 'this' from the caller by default
PR-URL: #42177 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 70c0758 commit 69a3792
Copy full SHA for 69a3792

File tree

Expand file treeCollapse file tree

3 files changed

+28
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+28
-10
lines changed
Open diff view settings
Collapse file

‎doc/api/async_context.md‎

Copy file name to clipboardExpand all lines: doc/api/async_context.md
+8Lines changed: 8 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ added:
446446
- v14.8.0
447447
- v12.19.0
448448
changes:
449+
- version: REPLACEME
450+
pr-url: https://github.com/nodejs/node/pull/42177
451+
description: Changed the default when `thisArg` is undefined to use `this`
452+
from the caller.
449453
- version: v16.0.0
450454
pr-url: https://github.com/nodejs/node/pull/36782
451455
description: Added optional thisArg.
@@ -468,6 +472,10 @@ added:
468472
- v14.8.0
469473
- v12.19.0
470474
changes:
475+
- version: REPLACEME
476+
pr-url: https://github.com/nodejs/node/pull/42177
477+
description: Changed the default when `thisArg` is undefined to use `this`
478+
from the caller.
471479
- version: v16.0.0
472480
pr-url: https://github.com/nodejs/node/pull/36782
473481
description: Added optional thisArg.
Collapse file

‎lib/async_hooks.js‎

Copy file name to clipboardExpand all lines: lib/async_hooks.js
+14-9Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
ArrayPrototypeIndexOf,
66
ArrayPrototypePush,
77
ArrayPrototypeSplice,
8+
ArrayPrototypeUnshift,
89
FunctionPrototypeBind,
910
NumberIsSafeInteger,
1011
ObjectDefineProperties,
@@ -223,15 +224,19 @@ class AsyncResource {
223224
return this[trigger_async_id_symbol];
224225
}
225226

226-
bind(fn, thisArg = this) {
227+
bind(fn, thisArg) {
227228
validateFunction(fn, 'fn');
228-
const ret =
229-
FunctionPrototypeBind(
230-
this.runInAsyncScope,
231-
this,
232-
fn,
233-
thisArg);
234-
ObjectDefineProperties(ret, {
229+
let bound;
230+
if (thisArg === undefined) {
231+
const resource = this;
232+
bound = function(...args) {
233+
ArrayPrototypeUnshift(args, fn, this);
234+
return ReflectApply(resource.runInAsyncScope, resource, args);
235+
};
236+
} else {
237+
bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg);
238+
}
239+
ObjectDefineProperties(bound, {
235240
'length': {
236241
configurable: true,
237242
enumerable: false,
@@ -245,7 +250,7 @@ class AsyncResource {
245250
writable: true,
246251
}
247252
});
248-
return ret;
253+
return bound;
249254
}
250255

251256
static bind(fn, type, thisArg) {
Collapse file

‎test/parallel/test-asyncresource-bind.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-asyncresource-bind.js
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ const fn3 = asyncResource.bind(common.mustCall(function() {
4141
fn3();
4242

4343
const fn4 = asyncResource.bind(common.mustCall(function() {
44-
assert.strictEqual(this, asyncResource);
44+
assert.strictEqual(this, undefined);
4545
}));
4646
fn4();
4747

4848
const fn5 = asyncResource.bind(common.mustCall(function() {
4949
assert.strictEqual(this, false);
5050
}), false);
5151
fn5();
52+
53+
const fn6 = asyncResource.bind(common.mustCall(function() {
54+
assert.strictEqual(this, 'test');
55+
}));
56+
fn6.call('test');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.