When a JavaScript interview asks for the output order, the useful answer is how you got there. Which code runs now? Which callback gets a turn later? And when that callback finally runs, which variable does it read? This guide uses a modern browser running one script, with no other code interacting with the snippets. Do not carry browser-specific scheduling assumptions into every Node.js version or calling context.

Read the captured binding, not a frozen value

1. What does a closure remember?

let status = 'queued';
const showStatus = () => console.log(status);
status = 'ready';
showStatus();

This logs ready. The function retains access to its lexical environment and reads the binding when it is called. It did not save a separate copy of the value inside the function. If you need the earlier string, pass the current value to a function that creates a callback from its own parameter. That gives the new callback a separate binding. With an object, the copied argument value still refers to that object; it does not freeze its properties.

2. Why do callbacks from a loop sometimes print the same number?

const callbacks = [];
for (let index = 0; index < 3; index++) {
  callbacks.push(() => index);
}
console.log(callbacks.map(read => read()));

With let, the loop creates a per-iteration binding, so the result is [0, 1, 2]. Change it to var in this example and all three callbacks read one shared binding after the loop finishes, returning [3, 3, 3]. No scheduling is needed to reproduce this bug. The same issue can show up in asynchronous callbacks because they also read the bindings after the loop advances. As a follow-up, try invoking the callbacks inside the loop and explain why the result changes.

Mark what runs now and what queues work

3. What prints first: the promise reaction or the timer?

console.log('start');
setTimeout(() => console.log('timer'), 0);
new Promise(resolve => {
  console.log('executor');
  resolve();
}).then(() => console.log('reaction'));
console.log('end');

The relative order of these logs is start, executor, end, reaction, timer. A promise constructor calls its executor synchronously. Its then handler runs later as a microtask. The browser processes microtasks after the current script's synchronous work and before taking another task, such as the timer callback. A zero-millisecond timeout does not mean “interrupt the script now” or guarantee the delay until execution. If a reaction enqueues another microtask, it can also run during the same checkpoint, before the timer.

4. Does awaiting an already fulfilled promise keep everything synchronous?

async function run() {
  console.log('before await');
  await Promise.resolve();
  console.log('after await');
}
run();
console.log('outside');

This logs before await, outside, then after await. Calling the async function starts executing it immediately up to the suspension point. Even for a fulfilled promise, the continuation after await is scheduled asynchronously. That suspension does not block the rest of the script, and it does not move CPU-heavy work to another thread. If the function performs a long synchronous calculation first, the caller still waits for that calculation to yield control.

5. Does Promise.all start work, order results, or cancel failures?

Those are three different questions. In Promise.all([loadProfile(), loadSettings()]), evaluating the arguments calls both functions; Promise.all combines the resulting promises. If both succeed, the output array follows the input order, not the completion order. If either rejects, the aggregate rejects without waiting for the other to succeed, but it does not cancel the underlying work. Cancellation needs explicit support, such as passing a shared AbortSignal to operations that honor it. Use Promise.allSettled when you need both outcomes even if one fails. If settings cannot be loaded until you know the profile's ID, sequential awaits express the real dependency rather than a performance mistake.

Practice making a prediction before running the code

  • Draw separate columns for synchronous logs, queued microtasks, and timer tasks. Mark when each becomes eligible to run.
  • Add a queueMicrotask before the then callback is registered, then move it after. Predict which reaction runs first each time.
  • Combine the first and third examples. Reassign the captured binding after scheduling a callback, then explain which value the callback reads.
  • Describe one case where independent calls should start together and another where the second call must wait for the first result.

For a runtime-specific next step, read the senior Node.js interview guide. To practice the answer-before-reveal habit, try Mentaro's free practice demo with a sample card.

Sources