Async context
AsyncLocalStorage.bind()
Returns a function bound to the execution context at binding time. Useful when handing a callback to a scheduler that otherwise executes it outside the desired scope. Release references when the callback is no longer needed. Tested on Node.js 22.16.0. Example output (one line per entry): ["saved"].
Formal Syntax & Structure
nodejs syntaxnodejs
AsyncLocalStorage.bind(fn)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fn | Function | Callback to bind. |
Return Value
Function
Context-bound callback.
Working Example
Example Snippetnodejs
import { AsyncLocalStorage } from 'node:async_hooks';const als = new AsyncLocalStorage();try { const fn = als.run('saved', () => AsyncLocalStorage.bind(() => als.getStore())); console.log(als.run('other', fn));}finally { als.disable();}