mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 07:19:19 +00:00

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>
29 lines
783 B
JavaScript
29 lines
783 B
JavaScript
'use strict';
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
|
|
async function main() {
|
|
const asyncLocalStorage = new AsyncLocalStorage();
|
|
const err = new Error();
|
|
const next = () => Promise.resolve()
|
|
.then(() => {
|
|
assert.strictEqual(asyncLocalStorage.getStore().get('a'), 1);
|
|
throw err;
|
|
});
|
|
await new Promise((resolve, reject) => {
|
|
asyncLocalStorage.run(() => {
|
|
const store = asyncLocalStorage.getStore();
|
|
store.set('a', 1);
|
|
next().then(resolve, reject);
|
|
});
|
|
})
|
|
.catch((e) => {
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
assert.strictEqual(e, err);
|
|
});
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
}
|
|
|
|
main();
|