How Async Context works and its limitations
mainBy default, unctx context is only available in synchronous execution and only before the first await statement. This ensures that context is not accidentally shared between concurrent asynchronous calls.
If you attempt to use the context after an await or inside a setTimeout, it will return null (or throw if using use).
Workaround: Cache the context in a local variable at the start of your function:
async function setup() {
const ctx = useAwesome(); // Cache the context immediately
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(ctx); // Still works because it's a local variable
}async function setup() {
console.log(useAwesome()); // Returns context
setTimeout(() => {
console.log(useAwesome());
}, 1); // Returns null
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(useAwesome()); // Returns null
}