Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
JavaScript runs synchronous code on the call stack, while asynchronous work is handled by browser or Node.js APIs and scheduled into queues. Promise callbacks go into the microtask queue, which is processed before the macrotask queue where `setTimeout` callbacks are scheduled.
JavaScript execution is single-threaded in the sense that one JavaScript call stack executes code at a time. The runtime also provides APIs for timers, network requests, DOM events, and other asynchronous operations.
When synchronous code finishes and the call stack becomes empty, the event loop checks the queues. Microtasks, including Promise reactions and queueMicrotask, are processed before the runtime moves on to the next macrotask such as a timer callback.
For example:
console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');The output is:
A
D
C
BsetTimeout(..., 0) does not mean "execute immediately." It means the timer becomes eligible after its minimum delay. The callback still has to wait for the current execution and higher-priority microtasks to finish.
A strong mid-level answer should also distinguish the JavaScript language from the host runtime. The event loop behavior is provided by the runtime environment, such as a browser or Node.js, while JavaScript defines mechanisms such as Promises and their microtask semantics.