What is the output of this Node.js code: console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4')?
1, 4, 3, 2. Sync code runs first (1, 4), then microtasks (promise callback 3), then macrotasks (setTimeout callback 2). This demonstrates event loop order: sync, then microtasks, then macrotasks.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Real-World Event Loop Examples in Node.js
fs.readFile does not block: the main thread continues, other requests are processed while the file reads, and the callback runs when it completes. fs.readFileSync blocks: the event loop stops until the file is read. Promise.all runs three API calls in parallel instead of sequentially.
Three independent API calls awaited sequentially take 3 times the longest. With Promise.all, they run in parallel, taking only as long as the slowest. The event loop processes all three concurrently, demonstrating non-blocking I/O.
A heavy computation on the main thread blocks all requests. Moving it to a worker_thread runs it on a separate thread, keeping the main thread's event loop free for other requests. This shows how CPU-heavy work is offloaded from the event loop.
Still have questions?
Browse all our FAQs or reach out to our support team
