Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Explicit resource management for AsyncLocalStorage #58651

Description

@everett1992

What is the problem this feature will solve?

Does it make sense to add a method to AsyncLocalStorage that returns Disposable?

What is the feature you are proposing to solve the problem?

This is the example from AsyncLocalStorage using .run, which introduces a callback and a level of nesting that comes with that.

http.createServer((req, res) => {
  asyncLocalStorage.run(idSeq++, () => {
    logWithId('start');
    // Imagine any chain of async operations here
    setImmediate(() => {
      logWithId('finish');
      res.end();
    });
  });
})

With with explicit resource management this could be:

http.createServer((req, res) => {
  using _id = asyncLocalStorage._PROPOSED(idSeq++)

  logWithId('start');
  // Imagine any chain of async operations here
  setImmediate(() => {
    logWithId('finish');
    res.end();
  });
})

enterWith currently returns undefined, could that be updated to return a Disposable?

What alternatives have you considered?

This could be built in user-land, I think

function enterWith<T>(als: AsyncLocalStorage<T>, store: T) {
  const oldStore = als.getStore()
  console.debug(als.enterWith(store))
  const dispose = new DisposableStack()
  dispose.defer(() => als.enterWith(oldStore))
  return dispose
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions