Closed
Description
📗 API Reference Docs Problem
- Version: ✍️12x , 13x, 14x, 15x
- Platform: ✍️ Not applicable
- Subsystem: async_hooks
Location
Section of the site where the content exists
Affected URL(s):
- https://github.com/nodejs/node/blob/master/doc/api/async_hooks.md#asynclocalstoragerunstore-callback-args
- https://nodejs.org/api/async_hooks.html#async_hooks_asynclocalstorage_run_store_callback_args
- https://nodejs.org/docs/latest-v14.x/api/async_hooks.html#async_hooks_asynclocalstorage_run_store_callback_args
- https://nodejs.org/docs/latest-v13.x/api/async_hooks.html#async_hooks_asynclocalstorage_run_store_callback_args
- https://nodejs.org/docs/latest-v12.x/api/async_hooks.html#async_hooks_asynclocalstorage_run_store_callback_args
Description
In the docs, asyncLocalStorage.run()
is described as follow:
This methods runs a function synchronously within a context and return its return value. The store is not accessible outside of the callback function or the asynchronous operations created within the callback.
However, the part where the asynchronous operation created within the callback is not accurate.
Referencing from one of the tests from Nodejs repo here
setTimeout(() => {
asyncLocalStorage.run(new Map(), () => {
asyncLocalStorage2.run(new Map(), () => {
const store = asyncLocalStorage.getStore();
const store2 = asyncLocalStorage2.getStore();
store.set('hello', 'world');
store2.set('hello', 'foo');
setTimeout(() => {
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'world');
assert.strictEqual(asyncLocalStorage2.getStore().get('hello'), 'foo');
asyncLocalStorage.exit(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
assert.strictEqual(asyncLocalStorage2.getStore().get('hello'), 'foo');
});
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'world');
assert.strictEqual(asyncLocalStorage2.getStore().get('hello'), 'foo');
}, 200);
});
});
}, 100);
the store is still accessible within the callback of the setTimeout()
, which is asynchronous.
I've tested with a simple fetch, and the store is accessible as intended
https://replit.com/@Darkripper214/ALS
I would conclude that an update to the phrase is warranted.
- I would like to work on this issue and
submit a pull request.