[{"id":"nodejsintermediate-001","courseId":"nodejs-intermediate","domain":"event-loop-async","topic":"process.nextTick queue ordering","prompt":"A script finishes its synchronous code. During that code it queued a process.nextTick() callback and a setImmediate() callback. Put these steps in the order Node.js runs them.","hint":"Ask which queue Node drains first once the current operation completes.","explain":"process.nextTick() keeps its own queue, outside the event loop's phases. setImmediate() is handled by a loop phase. Think about when each one gets its turn relative to the loop.","source":"https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick","interaction":{"type":"sort","items":[{"text":"The script's synchronous code finishes","why":"Every later step waits for the current operation to complete."},{"text":"process.nextTick() callbacks run","why":"The nextTick queue is processed after the current operation, whatever phase the loop is in."},{"text":"The event loop continues to its next step","why":"The loop does not proceed until every process.nextTick() callback has been resolved."},{"text":"The setImmediate() callback runs","why":"setImmediate() fires on the following iteration of the event loop, after the loop has moved on."}]},"options":[],"answer":"o:abcd","explanation":"After the current operation, Node.js drains the process.nextTick() queue before the event loop continues; setImmediate() callbacks run on a later loop iteration.","concept":"After the current operation, Node.js drains the process.nextTick() queue before the event loop continues; setImmediate() callbacks run on a later loop iteration.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["process.nextTick() callbacks run before the event loop continues, so recursive calls can starve I/O."]},{"id":"nodejsintermediate-002","courseId":"nodejs-intermediate","domain":"event-loop-async","topic":"CPU-heavy work blocking the event loop","prompt":"A request handler wants to run work without tying up the event loop. Which TWO of these operations are handed to the libuv Worker Pool rather than run on the event loop thread?","hint":"Only some built-in APIs are handed to a pool of background threads; ordinary JavaScript never is.","explain":"Node.js runs JavaScript callbacks on a single event loop thread. A small set of expensive operations is handed to a separate pool of worker threads, so the loop can keep serving other work.","source":"https://nodejs.org/en/learn/asynchronous-work/dont-block-the-event-loop","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"crypto.pbkdf2()","why":"crypto.pbkdf2() is one of the CPU-intensive crypto APIs that Node.js runs on the Worker Pool."},{"id":"b","text":"dns.lookup()","why":"dns.lookup() is one of the DNS APIs that Node.js runs on the Worker Pool."},{"id":"c","text":"fs.readFileSync()","whyNot":"Explicitly synchronous file system calls do not use the Worker Pool; they run on the thread that calls them."},{"id":"d","text":"JSON.parse() on a very large string","whyNot":"JavaScript code, including JSON.parse(), runs on the event loop thread; only listed APIs and C++ add-on tasks go to the pool."},{"id":"e","text":"socket.write() on a TCP connection","whyNot":"Network I/O is fulfilled by the event loop itself, not by the Worker Pool."},{"id":"f","text":"A loop that sums a ten-million-item array","whyNot":"Plain JavaScript loops run on the event loop, so they block it until they finish."}],"answer":"ab","explanation":"JavaScript callbacks run on the single event loop thread; only specific APIs, such as some crypto, zlib, file system and DNS calls, run on the libuv Worker Pool.","concept":"JavaScript callbacks run on the single event loop thread; only specific APIs, such as some crypto, zlib, file system and DNS calls, run on the libuv Worker Pool.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Only a fixed set of APIs uses the Worker Pool; plain JavaScript work, such as JSON.parse(), still blocks the event loop."]},{"id":"nodejsintermediate-003","courseId":"nodejs-intermediate","domain":"event-loop-async","topic":"setImmediate versus setTimeout in an I/O callback","prompt":"Inside a fs.readFile() callback, a script calls setTimeout(fn, 0) and then setImmediate(fn2). Which callback runs first?","hint":"The answer depends on where the callbacks were scheduled from, not on the delay value.","explain":"Where a callback is scheduled from changes when the event loop runs it. Callbacks scheduled inside an I/O completion behave differently from those scheduled in the main script.","source":"https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick","interaction":{"type":"single"},"options":[{"id":"a","text":"The setImmediate() callback, fn2","why":"Inside an I/O cycle, setImmediate() callbacks run before any timer, whatever order they were scheduled in."},{"id":"b","text":"The setTimeout() callback, fn, because it was scheduled first","whyNot":"Scheduling order doesn't decide this inside an I/O cycle; the immediate goes first."},{"id":"c","text":"Both, in the same batch, in the order they were scheduled","whyNot":"This ignores the I/O rule: an immediate scheduled inside an I/O cycle always runs before any timer."},{"id":"d","text":"The order varies from run to run, as it does in the main module","whyNot":"Run-to-run variation is the main-module case. Inside an I/O callback, the order is fixed."}],"answer":"a","explanation":"Inside an I/O cycle, setImmediate() always runs before any setTimeout() callback, independently of how many timers are present.","concept":"Inside an I/O cycle, setImmediate() always runs before any setTimeout() callback, independently of how many timers are present.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Inside an I/O callback, setImmediate() runs before a zero-delay setTimeout(), so chunked work can yield predictably."]},{"id":"nodejsintermediate-004","courseId":"nodejs-intermediate","domain":"event-loop-async","topic":"setInterval and clearInterval","prompt":"Calling setInterval(tick, 1000) runs tick about once per second until clearInterval() is called with the timer that setInterval() returned.","hint":"Think about how many times a timer fires, and what it takes to stop one.","explain":"Node.js timer functions come in one-shot and repeating forms. A repeating timer keeps firing until something cancels it, so the handle returned when you schedule it matters.","source":"https://nodejs.org/api/timers.html#setintervalcallback-delay-args","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"setInterval() repeats the callback at the given delay, and clearInterval() with the returned timer stops it."},{"id":"b","text":"False","whyNot":"It does not stop after one run; that one-shot behavior belongs to setTimeout()."}],"answer":"a","explanation":"setInterval() repeats its callback at a fixed delay until the returned Timeout is cancelled with clearInterval().","concept":"setInterval() repeats its callback at a fixed delay until the returned Timeout is cancelled with clearInterval().","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A setInterval() timer keeps firing until you call clearInterval() with the timer it returned."]},{"id":"nodejsintermediate-005","courseId":"nodejs-intermediate","domain":"event-loop-async","topic":"Unref timers","prompt":"A script schedules work with setImmediate() and then calls unref() on the returned Immediate, as shown. What does that call change?","hint":"Look at what the ref and unref methods control on an Immediate.","explain":"By default, a scheduled Immediate keeps the Node.js event loop running while it is active. The object returned by setImmediate() has a pair of methods that change this default.","source":"https://nodejs.org/api/timers.html#timeoutunref","interaction":{"type":"single"},"options":[{"id":"a","text":"It stops the Immediate from keeping the event loop alive","why":"After unref(), hasRef() returns false, so the Immediate no longer holds the event loop open by itself."},{"id":"b","text":"It cancels the Immediate, so the work never runs","whyNot":"Cancelling is clearImmediate()'s job. unref() only changes whether the Immediate keeps the loop running."},{"id":"c","text":"It moves the Immediate to run after all pending I/O","whyNot":"unref() doesn't change when the callback runs; it only changes whether the Immediate keeps the loop running."},{"id":"d","text":"It makes the work run synchronously, at the call site","whyNot":"The callback still runs later on the event loop; unref() does not make it synchronous."}],"answer":"a","explanation":"An Immediate is ref'd by default and keeps the event loop running; immediate.unref() removes that hold, and immediate.ref() restores it.","concept":"An Immediate is ref'd by default and keeps the event loop running; immediate.unref() removes that hold, and immediate.ref() restores it.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"const job = setImmediate(work);\njob.unref();","takeaways":["immediate.unref() lets the process exit without waiting for that Immediate; it does not cancel it."]}]