-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add AsyncLocalStorage.bind() and .snapshot()
PR-URL: #46387 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
- Loading branch information
1 parent
247caac
commit f1b7982
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
|
||
[1, false, '', {}, []].forEach((i) => { | ||
assert.throws(() => AsyncLocalStorage.bind(i), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
}); | ||
|
||
const fn = common.mustCall(AsyncLocalStorage.bind(() => 123)); | ||
assert.strictEqual(fn(), 123); | ||
|
||
const fn2 = AsyncLocalStorage.bind(common.mustCall((arg) => assert.strictEqual(arg, 'test'))); | ||
fn2('test'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { strictEqual } = require('assert'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
|
||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
const runInAsyncScope = | ||
asyncLocalStorage.run(123, common.mustCall(() => AsyncLocalStorage.snapshot())); | ||
const result = | ||
asyncLocalStorage.run(321, common.mustCall(() => { | ||
return runInAsyncScope(() => { | ||
return asyncLocalStorage.getStore(); | ||
}); | ||
})); | ||
strictEqual(result, 123); |