File tree Expand file tree Collapse file tree 2 files changed +25
-12
lines changed Expand file tree Collapse file tree 2 files changed +25
-12
lines changed Original file line number Diff line number Diff line change @@ -257,14 +257,11 @@ class AsyncLocalStorage {
257257 }
258258
259259 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 ) ;
264263 return callback ( ...args ) ;
265- } finally {
266- resource [ this . kResourceStore ] = outerStore ;
267- }
264+ } ) ;
268265 }
269266
270267 exitSyncAndReturn ( callback , ...args ) {
@@ -287,11 +284,10 @@ class AsyncLocalStorage {
287284 }
288285
289286 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+ } ) ;
295291 }
296292
297293 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