Adding AsyncLocalStorage class to async_hooks module. This API provide a simple CLS-like set of features. Co-authored-by: Andrey Pechkurov <apechkurov@gmail.com> PR-URL: https://github.com/nodejs/node/pull/26540 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
28 lines
755 B
JavaScript
28 lines
755 B
JavaScript
'use strict';
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
|
|
async function foo() {}
|
|
|
|
const asyncLocalStorage = new AsyncLocalStorage();
|
|
|
|
async function testOut() {
|
|
await foo();
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
}
|
|
|
|
async function testAwait() {
|
|
await foo();
|
|
assert.notStrictEqual(asyncLocalStorage.getStore(), undefined);
|
|
assert.strictEqual(asyncLocalStorage.getStore().get('key'), 'value');
|
|
await asyncLocalStorage.exitSyncAndReturn(testOut);
|
|
}
|
|
|
|
asyncLocalStorage.run(() => {
|
|
const store = asyncLocalStorage.getStore();
|
|
store.set('key', 'value');
|
|
testAwait(); // should not reject
|
|
});
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|