[{"id":"nodejsadvanced-001","courseId":"nodejs-advanced","domain":"event-loop-async","topic":"Event loop phase order","prompt":"A 100 ms timer is scheduled, then fs.readFile() starts and takes 95 ms. Put these steps of the event loop's handling of that work in the order they happen.","hint":"Notice which phase waits while the file is being read, and which phase runs the timer.","explain":"The loop cycles through phases, each with its own queue. The poll phase can wait for I/O, but only up to the nearest timer's threshold. Timer callbacks run only in the timers phase.","source":"https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick","interaction":{"type":"sort","items":[{"text":"The poll phase finds an empty queue and waits for the read","why":"The read hasn't finished, so poll has nothing to run and waits for the remaining time until the soonest timer is due."},{"text":"The finished readFile callback runs in the poll phase","why":"The read completes during the wait, and its callback is queued in poll and executed there."},{"text":"The loop wraps back to the timers phase","why":"The poll queue is now empty and the timer's threshold has been reached, so the loop returns to timers."},{"text":"The 100 ms timer callback runs","why":"Timer callbacks execute only in the timers phase, so this happens after the loop wraps back."}]},"options":[],"answer":"o:abcd","explanation":"The poll phase waits for I/O only until the soonest timer is due. Once its queue is empty, the loop wraps back to the timers phase to run the timer.","concept":"The poll phase waits for I/O only until the soonest timer is due. Once its queue is empty, the loop wraps back to the timers phase to run the timer.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A due timer runs when the loop returns to the timers phase, not the instant its threshold passes."]},{"id":"nodejsadvanced-002","courseId":"nodejs-advanced","domain":"event-loop-async","topic":"UV_THREADPOOL_SIZE","prompt":"A service makes many slow fs.readFile() calls. Its unrelated dns.lookup() calls also slow down, although they touch no files. Which explanation fits Node.js?","hint":"Several different APIs may share one fixed resource inside libuv.","explain":"Some Node.js calls lack an asynchronous system API, so libuv runs them on a threadpool of worker threads. That pool has a fixed size set at startup, which limits how much of this work runs in parallel.","source":"https://nodejs.org/api/cli.html#uv_threadpool_sizesize","interaction":{"type":"single"},"options":[{"id":"a","text":"dns.lookup() bypasses libuv, so fs reads cannot affect it","whyNot":"dns.lookup() is on the threadpool list, so it competes for the same fixed threads as fs calls."},{"id":"b","text":"Each fs.readFile() occupies the main thread until the read completes","whyNot":"Threadpool work runs on worker threads off the main thread, so a pending read does not block JavaScript."},{"id":"c","text":"Both queue for libuv's fixed-size threadpool, so slow file work delays DNS work","why":"fs calls and dns.lookup() share the same small set of threads, so a backlog of file reads holds up lookups."},{"id":"d","text":"Each fs.readFile() gets a new OS thread, which exhausts the thread limit","whyNot":"The threadpool is a fixed set of threads reused across tasks, not one new thread per call."}],"answer":"c","explanation":"libuv's threadpool has a fixed size and serves fs, dns.lookup(), zlib and some async crypto APIs, so slow tasks can delay the others. UV_THREADPOOL_SIZE raises the count above its default of 4.","concept":"libuv's threadpool has a fixed size and serves fs, dns.lookup(), zlib and some async crypto APIs, so slow tasks can delay the others. UV_THREADPOOL_SIZE raises the count above its default of 4.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Slow fs or crypto work can slow dns.lookup() because they share libuv's threadpool."]},{"id":"nodejsadvanced-003","courseId":"nodejs-advanced","domain":"event-loop-async","topic":"AsyncLocalStorage context propagation","prompt":"An HTTP handler wraps each request in asyncLocalStorage.run(requestId, ...), then schedules follow-up work with setImmediate(). Which TWO statements about the store are true?","hint":"Think about what the store is tied to: the run() call and the async work it starts.","explain":"AsyncLocalStorage attaches a value to a chain of asynchronous work instead of using a global variable. A store exists inside run() or after enterWith(), and it is carried through the async work that run() starts.","source":"https://nodejs.org/api/async_context.html#class-asynclocalstorage","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"A setImmediate() callback scheduled inside run() still sees the requestId.","why":"The store stays coherent through async operations started inside the run() callback, so the value carries into the immediate."},{"id":"b","text":"A setImmediate() callback sees undefined, because timers run outside the store.","whyNot":"The store follows async work started inside run(), so the immediate callback still sees the value."},{"id":"c","text":"Two AsyncLocalStorage instances keep separate stores, so run() on one leaves the other alone.","why":"Each instance maintains an independent storage context, so instances do not interfere with each other."},{"id":"d","text":"Two AsyncLocalStorage instances share one store, so a run() on either overwrites the other.","whyNot":"Each instance has its own context; stores are not shared between instances."},{"id":"e","text":"Outside any run() call, getStore() returns the value from the most recent run().","whyNot":"A store exists only inside run() or after enterWith(); outside those, getStore() returns undefined unless a defaultValue was set."},{"id":"f","text":"You can skip disable(), since the instance is collected as soon as run() returns.","whyNot":"Calling disable() is required before the instance can be garbage collected."}],"answer":"ac","explanation":"AsyncLocalStorage keeps its store coherent across async operations started inside run(). Each instance is independent, and disable() is needed before the instance can be garbage collected.","concept":"AsyncLocalStorage keeps its store coherent across async operations started inside run(). Each instance is independent, and disable() is needed before the instance can be garbage collected.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Stores follow the async work that run() starts, and each AsyncLocalStorage instance is independent."]},{"id":"nodejsadvanced-004","courseId":"nodejs-advanced","domain":"event-loop-async","topic":"AsyncResource for custom resources","prompt":"In a class that extends AsyncResource, calling this.runInAsyncScope(callback, null, err, data) runs callback in the resource's async context, with the async hooks' before and after callbacks fired around it.","hint":"Look at what runInAsyncScope does besides calling the function it is given.","explain":"AsyncResource lets your own resource types take part in async context tracking and async hooks. A dedicated method runs a callback in that resource's context, and its effects go beyond simply calling the function.","source":"https://nodejs.org/api/async_context.html#class-asyncresource","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"runInAsyncScope establishes the resource's context, triggers the before hooks, calls the function, triggers the after hooks, then restores the original context."},{"id":"b","text":"False","whyNot":"The method does establish the resource's context and fire the before and after hooks, so the statement holds."}],"answer":"a","explanation":"runInAsyncScope establishes an AsyncResource's context, fires AsyncHooks before and after callbacks around the call, and then restores the original context.","concept":"runInAsyncScope establishes an AsyncResource's context, fires AsyncHooks before and after callbacks around the call, and then restores the original context.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Route callbacks for a custom resource through runInAsyncScope to keep their async context."]},{"id":"nodejsadvanced-005","courseId":"nodejs-advanced","domain":"event-loop-async","topic":"async_hooks lifecycle callbacks","prompt":"The hook below is meant to trace async resources. Once it is enabled, the same init callback keeps firing and the program never finishes. Why?","hint":"Consider whether console.log() is synchronous, and what asynchronous work it might start.","explain":"async_hooks callbacks fire for the init, before, after and destroy events of every async operation. They run at sensitive points in an object's life, so any async work they start feeds back into the hook.","source":"https://nodejs.org/api/async_hooks.html","interaction":{"type":"single"},"options":[{"id":"a","text":"console.log() is blocked while the hook runs, so its buffer fills and the program stalls.","whyNot":"The hook doesn't block output; the trouble is that the logging itself creates new asynchronous events."},{"id":"b","text":"console.log() is itself asynchronous, so each call fires init again and the hook recurses without end.","why":"Logging inside an init callback creates new async work, which calls the hook again, forever."},{"id":"c","text":"init() runs only once per process, so the second console.log() call throws an error.","whyNot":"init() is called for each async resource, not once per process, so this is not the cause."},{"id":"d","text":"Hook callbacks must be async functions so they can await console.log().","whyNot":"Hook callbacks must not be async functions that return promises; the failure comes from the recursion caused by logging."}],"answer":"b","explanation":"console.log() is asynchronous, so calling it inside an AsyncHook callback triggers more callbacks and infinite recursion. A synchronous write such as fs.writeFileSync() avoids this.","concept":"console.log() is asynchronous, so calling it inside an AsyncHook callback triggers more callbacks and infinite recursion. A synchronous write such as fs.writeFileSync() avoids this.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"import async_hooks from 'node:async_hooks';\n\nasync_hooks.createHook({\n  init(id, type) {\n    console.log(type, id);\n  },\n}).enable();","takeaways":["Debug AsyncHook callbacks with a synchronous write such as fs.writeFileSync()."]}]