Imperatively sets context as active for the current async execution chain
and operations spawned from it. Returns a ContextManagementToken whose dispose()
restores the previous context (see ContextManager.attach).
On Node.js 25.9+, delegates to AsyncLocalStorage.withScope() which returns
a native RunScope. On older Node.js versions, falls back to enterWith() with
a manual token.
Caveat for async functions: Both withScope() and enterWith() affect the
entire current async execution chain. If attach() is called inside an async
function before the first await, the context change will leak into the caller's
context and remain active there until something else restores it. Prefer with()
for async code.
This API is experimental and may change in minor releases without prior notice.
a function or event emitter. When target or one of its callbacks is called,
the provided context will be used as the active context for the duration of the call.
Imperatively sets
contextas active for the current async execution chain and operations spawned from it. Returns a ContextManagementToken whosedispose()restores the previous context (see ContextManager.attach).On Node.js 25.9+, delegates to
AsyncLocalStorage.withScope()which returns a nativeRunScope. On older Node.js versions, falls back toenterWith()with a manual token.Caveat for async functions: Both
withScope()andenterWith()affect the entire current async execution chain. Ifattach()is called inside an async function before the firstawait, the context change will leak into the caller's context and remain active there until something else restores it. Preferwith()for async code.This API is experimental and may change in minor releases without prior notice.