File tree 3 files changed +37
-6
lines changed
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 {
730
730
}
731
731
```
732
732
733
- #### Static method: ` AsyncResource.bind(fn[, type]) `
733
+ #### Static method: ` AsyncResource.bind(fn[, type, [thisArg] ]) `
734
734
<!-- YAML
735
735
added:
736
736
- v14.8.0
737
737
- v12.19.0
738
+ changes:
739
+ - version: REPLACEME
740
+ pr-url: https://github.com/nodejs/node/pull/36782
741
+ description: Added optional thisArg.
738
742
-->
739
743
740
744
* ` fn ` {Function} The function to bind to the current execution context.
741
745
* ` type ` {string} An optional name to associate with the underlying
742
746
` AsyncResource ` .
747
+ * ` thisArg ` {any}
743
748
744
749
Binds the given function to the current execution context.
745
750
746
751
The returned function will have an ` asyncResource ` property referencing
747
752
the ` AsyncResource ` to which the function is bound.
748
753
749
- #### ` asyncResource.bind(fn) `
754
+ #### ` asyncResource.bind(fn[, thisArg] ) `
750
755
<!-- YAML
751
756
added:
752
757
- v14.8.0
753
758
- v12.19.0
759
+ changes:
760
+ - version: REPLACEME
761
+ pr-url: https://github.com/nodejs/node/pull/36782
762
+ description: Added optional thisArg.
754
763
-->
755
764
756
765
* ` fn ` {Function} The function to bind to the current ` AsyncResource ` .
766
+ * ` thisArg ` {any}
757
767
758
768
Binds the given function to execute to this ` AsyncResource ` 's scope.
759
769
Original file line number Diff line number Diff line change @@ -220,10 +220,15 @@ class AsyncResource {
220
220
return this [ trigger_async_id_symbol ] ;
221
221
}
222
222
223
- bind ( fn ) {
223
+ bind ( fn , thisArg = this ) {
224
224
if ( typeof fn !== 'function' )
225
225
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 ) ;
227
232
ObjectDefineProperties ( ret , {
228
233
'length' : {
229
234
configurable : true ,
@@ -241,9 +246,9 @@ class AsyncResource {
241
246
return ret ;
242
247
}
243
248
244
- static bind ( fn , type ) {
249
+ static bind ( fn , type , thisArg ) {
245
250
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 ) ;
247
252
}
248
253
}
249
254
Original file line number Diff line number Diff line change @@ -33,3 +33,19 @@ setImmediate(() => {
33
33
assert . strictEqual ( asyncResource . asyncId ( ) , fn2 ( ) ) ;
34
34
assert . notStrictEqual ( asyncId , fn2 ( ) ) ;
35
35
} ) ;
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