async_hooks: add thisArg to AsyncResource.bind · nodejs/node@324a6c2 · GitHub
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 <jasnell@gmail.com> PR-URL: #36782 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent 7dea99b commit 324a6c2

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

doc/api/async_hooks.md

Lines changed: 12 additions & 2 deletions

lib/async_hooks.js

Lines changed: 9 additions & 4 deletions
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

test/parallel/test-asyncresource-bind.js

Lines changed: 16 additions & 0 deletions

0 commit comments

Comments
 (0)