Skip to content

Commit 4588666

Browse files
committed
async_hooks: deprecate the AsyncResource.bind asyncResource property
Runtime-deprecates the `asyncResource` property that is attached to the wrapper function returned by `asyncResource.bind()`. This property is not expected to align with the equivalent `asyncContext.wrap()` API in the proposed TC39 standard.
1 parent 9e7093f commit 4588666

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

doc/api/async_context.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ added:
466466
- v14.8.0
467467
- v12.19.0
468468
changes:
469+
- version: REPLACEME
470+
pr-url: https://github.com/nodejs/node/pull/46432
471+
description: The `asyncResource` property added to the bound function
472+
has been deprecated and will be removed in a future
473+
version.
469474
- version:
470475
- v17.8.0
471476
- v16.15.0
@@ -484,16 +489,18 @@ changes:
484489

485490
Binds the given function to the current execution context.
486491

487-
The returned function will have an `asyncResource` property referencing
488-
the `AsyncResource` to which the function is bound.
489-
490492
### `asyncResource.bind(fn[, thisArg])`
491493

492494
<!-- YAML
493495
added:
494496
- v14.8.0
495497
- v12.19.0
496498
changes:
499+
- version: REPLACEME
500+
pr-url: https://github.com/nodejs/node/pull/46432
501+
description: The `asyncResource` property added to the bound function
502+
has been deprecated and will be removed in a future
503+
version.
497504
- version:
498505
- v17.8.0
499506
- v16.15.0
@@ -510,9 +517,6 @@ changes:
510517

511518
Binds the given function to execute to this `AsyncResource`'s scope.
512519

513-
The returned function will have an `asyncResource` property referencing
514-
the `AsyncResource` to which the function is bound.
515-
516520
### `asyncResource.runInAsyncScope(fn[, thisArg, ...args])`
517521

518522
<!-- YAML

doc/api/deprecations.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,6 +3339,20 @@ In a future version of Node.js, [`message.headers`][],
33393339
[`message.headersDistinct`][], [`message.trailers`][], and
33403340
[`message.trailersDistinct`][] will be read-only.
33413341

3342+
### DEP0172: The `asyncResource` property of `AsyncResource` bound functions
3343+
3344+
<!-- YAML
3345+
changes:
3346+
- version: REPLACEME
3347+
pr-url: https://github.com/nodejs/node/pull/46432
3348+
description: Runtime-deprecation.
3349+
-->
3350+
3351+
Type: Runtime
3352+
3353+
In a future version of Node.js, the `asyncResource` property will no longer
3354+
be added when a function is bound to an `AsyncResource`.
3355+
33423356
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
33433357
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
33443358
[RFC 8247 Section 2.4]: https://www.rfc-editor.org/rfc/rfc8247#section-2.4

lib/async_hooks.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ const {
2020
ERR_ASYNC_TYPE,
2121
ERR_INVALID_ASYNC_ID
2222
} = require('internal/errors').codes;
23-
const { kEmptyObject } = require('internal/util');
23+
const {
24+
deprecate,
25+
kEmptyObject,
26+
} = require('internal/util');
2427
const {
2528
validateFunction,
2629
validateObject,
@@ -238,6 +241,7 @@ class AsyncResource {
238241
} else {
239242
bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg);
240243
}
244+
let self = this;
241245
ObjectDefineProperties(bound, {
242246
'length': {
243247
__proto__: null,
@@ -250,8 +254,12 @@ class AsyncResource {
250254
__proto__: null,
251255
configurable: true,
252256
enumerable: true,
253-
value: this,
254-
writable: true,
257+
get: deprecate(function() {
258+
return self;
259+
}, 'The asyncResource property on bound functions is deprecated', 'DEP0172'),
260+
set: deprecate(function(val) {
261+
self = val;
262+
}, 'The asyncResource property on bound functions is deprecated', 'DEP0172'),
255263
}
256264
});
257265
return bound;

0 commit comments

Comments
 (0)