File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
lib/internal/async_local_storage Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,8 @@ class AsyncLocalStorage {
51
51
if ( options . name !== undefined ) {
52
52
this . #name = `${ options . name } ` ;
53
53
}
54
+
55
+ this . _enable ( ) ;
54
56
}
55
57
56
58
/** @type {string } */
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const common = require ( '../common' ) ;
3
+ const assert = require ( 'assert' ) ;
4
+ const { AsyncLocalStorage } = require ( 'async_hooks' ) ;
5
+
6
+ // Verify that `enterWith()` does not leak the store to the parent context in a promise.
7
+
8
+ const als = new AsyncLocalStorage ( ) ;
9
+
10
+ async function asyncFunctionAfterAwait ( ) {
11
+ await 0 ;
12
+ als . enterWith ( 'after await' ) ;
13
+ }
14
+
15
+ function promiseThen ( ) {
16
+ return Promise . resolve ( )
17
+ . then ( ( ) => {
18
+ als . enterWith ( 'inside then' ) ;
19
+ } ) ;
20
+ }
21
+
22
+ async function asyncFunctionBeforeAwait ( ) {
23
+ als . enterWith ( 'before await' ) ;
24
+ await 0 ;
25
+ }
26
+
27
+ async function main ( ) {
28
+ await asyncFunctionAfterAwait ( ) ;
29
+ await promiseThen ( ) ;
30
+ assert . strictEqual ( als . getStore ( ) , undefined ) ;
31
+
32
+ // This is a known limitation of the `enterWith` API.
33
+ await asyncFunctionBeforeAwait ( ) ;
34
+ assert . strictEqual ( als . getStore ( ) , 'before await' ) ;
35
+ }
36
+
37
+ main ( ) . then ( common . mustCall ( ) ) ;
You can’t perform that action at this time.
0 commit comments