mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 15:41:06 +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>
32 lines
847 B
JavaScript
32 lines
847 B
JavaScript
'use strict';
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
|
|
// case 2 using *AndReturn calls (dual behaviors)
|
|
const asyncLocalStorage = new AsyncLocalStorage();
|
|
|
|
let i = 0;
|
|
process.setUncaughtExceptionCaptureCallback((err) => {
|
|
++i;
|
|
assert.strictEqual(err.message, 'err2');
|
|
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'node');
|
|
});
|
|
|
|
try {
|
|
asyncLocalStorage.runSyncAndReturn(() => {
|
|
const store = asyncLocalStorage.getStore();
|
|
store.set('hello', 'node');
|
|
setTimeout(() => {
|
|
process.nextTick(() => {
|
|
assert.strictEqual(i, 1);
|
|
});
|
|
throw new Error('err2');
|
|
}, 0);
|
|
throw new Error('err1');
|
|
});
|
|
} catch (e) {
|
|
assert.strictEqual(e.message, 'err1');
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
}
|