Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumNodeJSasync_hooks for contextSingle-choice MCQ

Using async_hooks / AsyncLocalStorage to carry a request id, why does the value below come back undefined inside the setTimeout callback?

const { AsyncLocalStorage } = require('async_hooks'); const als = new AsyncLocalStorage(); als.run({ id: 'abc' }, () => { setTimeout(() => { console.log(als.getStore()?.id); }, 0); }); // Assume an UNRELATED setTimeout was scheduled BEFORE als.run from outside any store // and that callback is what runs here.