File tree 2 files changed +25
-12
lines changed
2 files changed +25
-12
lines changed Original file line number Diff line number Diff line change @@ -257,14 +257,11 @@ class AsyncLocalStorage {
257
257
}
258
258
259
259
runSyncAndReturn ( store , callback , ...args ) {
260
- const resource = executionAsyncResource ( ) ;
261
- const outerStore = resource [ this . kResourceStore ] ;
262
- this . enterWith ( store ) ;
263
- try {
260
+ const resource = new AsyncResource ( 'AsyncLocalStorage' ) ;
261
+ return resource . runInAsyncScope ( ( ) => {
262
+ this . enterWith ( store ) ;
264
263
return callback ( ...args ) ;
265
- } finally {
266
- resource [ this . kResourceStore ] = outerStore ;
267
- }
264
+ } ) ;
268
265
}
269
266
270
267
exitSyncAndReturn ( callback , ...args ) {
@@ -287,11 +284,10 @@ class AsyncLocalStorage {
287
284
}
288
285
289
286
run ( store , callback , ...args ) {
290
- const resource = executionAsyncResource ( ) ;
291
- const outerStore = resource [ this . kResourceStore ] ;
292
- this . enterWith ( store ) ;
293
- process . nextTick ( callback , ...args ) ;
294
- resource [ this . kResourceStore ] = outerStore ;
287
+ process . nextTick ( ( ) => {
288
+ this . enterWith ( store ) ;
289
+ return callback ( ...args ) ;
290
+ } ) ;
295
291
}
296
292
297
293
exit ( callback , ...args ) {
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ require ( '../common' ) ;
3
+ const assert = require ( 'assert' ) ;
4
+ const {
5
+ AsyncLocalStorage,
6
+ executionAsyncResource
7
+ } = require ( 'async_hooks' ) ;
8
+
9
+ const asyncLocalStorage = new AsyncLocalStorage ( ) ;
10
+
11
+ const outerResource = executionAsyncResource ( ) ;
12
+
13
+ asyncLocalStorage . run ( new Map ( ) , ( ) => {
14
+ assert . notStrictEqual ( executionAsyncResource ( ) , outerResource ) ;
15
+ } ) ;
16
+
17
+ assert . strictEqual ( executionAsyncResource ( ) , outerResource ) ;
You can’t perform that action at this time.
0 commit comments