Skip to content

doc: inconsistent asnycLocalStorage.run() description #38022

Closed
@PhakornKiong

Description

@PhakornKiong

📗 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):

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooksIssues and PRs related to the async hooks subsystem.docIssues and PRs related to the documentations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions