File tree Expand file tree Collapse file tree 3 files changed +37
-6
lines changed Expand file tree Collapse file tree 3 files changed +37
-6
lines changed Original file line number Diff line number Diff 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
735735added:
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
744749Binds the given function to the current execution context.
745750
746751The returned function will have an `asyncResource` property referencing
747752the `AsyncResource` to which the function is bound.
748753
749- #### `asyncResource.bind(fn)`
754+ #### `asyncResource.bind(fn[, thisArg] )`
750755<!-- YAML
751756added:
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
758768Binds the given function to execute to this `AsyncResource`'s scope.
759769
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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();
You can’t perform that action at this time.
0 commit comments