How do process.nextTick, promise microtasks, timers, and setImmediate interact across Node.js event loop phases, and how can their ordering cause starvation?
Answer out loud or in your own words before revealing the reference answer.
Answer out loud or in your own words before revealing the reference answer.
Node.js · Advanced
Node.js drains the nextTick queue before the promise microtask queue after the current JavaScript operation and between event loop callbacks. Timers run in the timers phase, while setImmediate callbacks run in the check phase. Their relative order can depend on the surrounding I/O context. Recursively scheduling nextTick callbacks can prevent the event loop from reaching I/O and timer phases, which starves other work.
Node.js · Expert
AsyncLocalStorage associates a store with an asynchronous execution chain using async hooks. Promises and standard Node.js callback APIs usually preserve it automatically. Context can be lost when libraries create unusual async boundaries, callbacks are invoked outside the captured resource, or enterWith is used across overlapping work. run should define a bounded request scope, and AsyncResource can bridge custom callback or event integrations.
Node.js · Advanced
A writable buffers chunks up to its highWaterMark. write returning false means the producer should stop writing, not that the chunk was rejected. The producer resumes after the drain event. Readable streams similarly stop pulling when push returns false. stream.pipeline coordinates flow control and propagates completion and errors across the chain.
TypeScript · Expert
A conditional type distributes when its checked type is a naked type parameter, so T extends U ? X : Y is evaluated for each union member. Wrapping both sides in tuples, such as [T] extends [U], tests the union as a whole. Distribution is useful for union mapping but surprising when an API intends to reject or preserve an entire union, especially with never because distributing over an empty union produces never.
Node.js · Expert
Workers help CPU-bound JavaScript and native work that would otherwise block the event loop; they rarely improve ordinary asynchronous I/O. A bounded pool should account for available CPU, container quotas, task duration, serialization or transfer costs, memory per isolate, queue limits, cancellation, and overload policy. Measure event loop delay, queue latency, utilization, memory, and end-to-end throughput before increasing concurrency.
TypeScript · Advanced
With strictFunctionTypes, standalone function parameters are checked contravariantly, preventing a callback that accepts only a narrower subtype from being used where a broader input may arrive. Method syntax remains bivariant for compatibility with common object and class patterns. This exception can admit assignments that fail at runtime, so callback-heavy public APIs can use function properties when stricter checking is desirable.