mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 20:29:23 +01:00
6510a741c4
This commit introduces store as the first argument in AsyncLocalStorage's run methods. The change is motivated by the following expectation: most users are going to use a custom object as the store and an extra Map created by the previous implementation is an overhead for their use case. Important note. This is a backwards incompatible change. It was discussed and agreed an incompatible change is ok since the API is still experimental and the modified methods were only added within the last week so usage will be minimal to none. PR-URL: https://github.com/nodejs/node/pull/31930 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
29 lines
794 B
JavaScript
29 lines
794 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(new Map(), () => {
|
|
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();
|