Skip to content

Commit 324a6c2

Browse files
committed
async_hooks: add thisArg to AsyncResource.bind
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: #36051 Signed-off-by: James M Snell <[email protected]> PR-URL: #36782 Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent 7dea99b commit 324a6c2

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

Diff for: doc/api/async_hooks.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -730,30 +730,40 @@ class DBQuery extends AsyncResource {
730730
}
731731
```
732732

733-
#### Static method: `AsyncResource.bind(fn[, type])`
733+
#### Static method: `AsyncResource.bind(fn[, type, [thisArg]])`
734734
<!-- YAML
735735
added:
736736
- v14.8.0
737737
- v12.19.0
738+
changes:
739+
- version: REPLACEME
740+
pr-url: https://github.com/nodejs/node/pull/36782
741+
description: Added optional thisArg.
738742
-->
739743

740744
* `fn` {Function} The function to bind to the current execution context.
741745
* `type` {string} An optional name to associate with the underlying
742746
`AsyncResource`.
747+
* `thisArg` {any}
743748

744749
Binds the given function to the current execution context.
745750

746751
The returned function will have an `asyncResource` property referencing
747752
the `AsyncResource` to which the function is bound.
748753

749-
#### `asyncResource.bind(fn)`
754+
#### `asyncResource.bind(fn[, thisArg])`
750755
<!-- YAML
751756
added:
752757
- v14.8.0
753758
- v12.19.0
759+
changes:
760+
- version: REPLACEME
761+
pr-url: https://github.com/nodejs/node/pull/36782
762+
description: Added optional thisArg.
754763
-->
755764

756765
* `fn` {Function} The function to bind to the current `AsyncResource`.
766+
* `thisArg` {any}
757767

758768
Binds the given function to execute to this `AsyncResource`'s scope.
759769

Diff for: lib/async_hooks.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,15 @@ class AsyncResource {
220220
return this[trigger_async_id_symbol];
221221
}
222222

223-
bind(fn) {
223+
bind(fn, thisArg = this) {
224224
if (typeof fn !== 'function')
225225
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
226-
const ret = FunctionPrototypeBind(this.runInAsyncScope, this, fn);
226+
const ret =
227+
FunctionPrototypeBind(
228+
this.runInAsyncScope,
229+
this,
230+
fn,
231+
thisArg);
227232
ObjectDefineProperties(ret, {
228233
'length': {
229234
configurable: true,
@@ -241,9 +246,9 @@ class AsyncResource {
241246
return ret;
242247
}
243248

244-
static bind(fn, type) {
249+
static bind(fn, type, thisArg) {
245250
type = type || fn.name;
246-
return (new AsyncResource(type || 'bound-anonymous-fn')).bind(fn);
251+
return (new AsyncResource(type || 'bound-anonymous-fn')).bind(fn, thisArg);
247252
}
248253
}
249254

Diff for: test/parallel/test-asyncresource-bind.js

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ setImmediate(() => {
3333
assert.strictEqual(asyncResource.asyncId(), fn2());
3434
assert.notStrictEqual(asyncId, fn2());
3535
});
36+
37+
const foo = {};
38+
const fn3 = asyncResource.bind(common.mustCall(function() {
39+
assert.strictEqual(this, foo);
40+
}), foo);
41+
fn3();
42+
43+
const fn4 = asyncResource.bind(common.mustCall(function() {
44+
assert.strictEqual(this, asyncResource);
45+
}));
46+
fn4();
47+
48+
const fn5 = asyncResource.bind(common.mustCall(function() {
49+
assert.strictEqual(this, false);
50+
}), false);
51+
fn5();

0 commit comments

Comments
 (0)