Node.js · Senior · Question 69 of 75
Why can AsyncLocalStorage lose its context inside some libraries, and what fixes it?
- 1
AsyncLocalStorageworks only in CommonJS - ✓Correct answer: Libraries that queue callbacks themselves (custom pools, batching) call them from another async context;
AsyncResource.bindorAsyncLocalStorage.bindrestores it - 3The context is cleared by the garbage collector every second
Explanation
Context follows the async chain where a callback was created. A pool that runs a stored callback later continues the chain of whoever triggered it, not the original request.