Skip to content

Explicit resource management for AsyncLocalStorage #58651

@everett1992

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions