Most devs “know” JavaScript. Far fewer can predict what the engine will do in gnarly closure + async + coercion puzzles.
- Promise + closure traps
- Event loop + async scope
Each snippet has:
- The output
- A short why, focused on the mental model behind it.
1) Promise + Closure Traps
1. Same Promise, Same Value
function run() {
let x = 1;
return Promise.resolve().then(() => x++);
}
const fn = run();
fn.then(console.log);
fn.then(console.log);Output:
1
1Why: run returns a Promise that resolves once, with the value of x++ at the time its .then runs. Both fn.then(...) attach to the same resolved promise, so they see the same resolved value (1). The post-increment side effect (x becoming 2) doesn’t change the already resolved value.
2. var in Loop with Promises
for (var i = 0; i < 3; i++) {
Promise.resolve().then(() => console.log(i));
}Output:
3
3
3Why: var is function scoped, so all .then callbacks close over the same i. By the time microtasks run, the loop is finished and i === 3, so each callback logs 3.
3. let in Loop with Promises
for (let i = 0; i < 3; i++) {
Promise.resolve().then(() => console.log(i));
}Output:
0
1
2Why: let creates a new binding per iteration. Each callback closes over its own i value, so you get 0, 1, 2 as expected.
4. Counter with Promise
function counter() {
let n = 0;
return () => Promise.resolve(n++);
}
const c = counter();
c().then(console.log);
c().then(console.log);Output:
0
1Why: n is in closure. First call resolves with 0 then increments to 1. Second call resolves with 1 then increments to 2.
5. Sync vs Microtask Ordering
let x = 10;
Promise.resolve().then(() => {
console.log(x);
x = 20;
});
console.log(x);Output:
10
10Why: The last line console.log(x) is synchronous and runs immediately, logging 10. The .then callback is a microtask that runs after the current call stack, still seeing x === 10 before it updates it.
6. Two Microtasks Updating Same Variable
let a = 1;
Promise.resolve().then(() => a++);
Promise.resolve().then(() => console.log(a));Output:
2Why: Both .then handlers are microtasks queued in order. First microtask runs, a becomes 2. Second microtask logs 2.
7. Increment in Closure
function build() {
let v = 0;
return () => Promise.resolve(++v);
}
const f = build();
f().then(console.log);
f().then(console.log);Output:
1
2Why: ++v increments before resolving. First call gives 1, second 2.
8. Chained .then
let x = 5;
Promise.resolve()
.then(() => x++)
.then(() => console.log(x));Output:
6Why: First .then runs, x becomes 6. Second .then logs current x → 6.
9. New Promise Each Call
let val = 0;
function inc() {
return new Promise(res => res(val++));
}
inc().then(console.log);
inc().then(console.log);Output:
0
1Why: Each inc() creates a new promise that resolves immediately with current val, then increments it.
10. Transforming Promise Result
const fn = () => Promise.resolve(1);
fn().then(v => v + 1).then(console.log);Output:
2Why: Promise resolves with 1, next handler returns 2, last console.log prints 2.
11. Two Microtasks, Post-Increment
let x = 1;
Promise.resolve().then(() => console.log(x++));
Promise.resolve().then(() => console.log(x++));Output:
1
2Why: First microtask logs 1, then x becomes 2. Second logs 2, then x becomes 3.
12. Synchronous Log vs .then
let a = 10;
new Promise(res => res()).then(() => {
a = 20;
});
console.log(a);Output:
10Why: console.log(a) runs immediately before the .then microtask runs. At that point a is still 10.
13. Shared count in Closure
let count = 0;
const fn = () => Promise.resolve(count++);
fn().then(console.log);
fn().then(console.log);Output:
0
1Why: Same pattern as val++: shared closure, new promise each time.
14. Nested Microtask
let x = 1;
Promise.resolve().then(() => {
Promise.resolve().then(() => console.log(x));
});
x = 2;Output:
2Why: Outer .then queues inner .then. The assignment x = 2 runs before either microtask, so inner logs 2 when it runs.
15. Two Microtasks, One Mutates
let v = 0;
Promise.resolve().then(() => v++);
Promise.resolve().then(() => console.log(v));Get Shivangi Dixit’s stories in your inbox
Join Medium for free to get updates from this writer.
Output:
1Why: First microtask increments v to 1. Second logs 1.
2) Event Loop + Async Scope
16. Sync vs Promise vs Timeout
console.log(1);
setTimeout(() => console.log(2));
Promise.resolve().then(() => console.log(3));
console.log(4);Output:
1
4
3
2Why: Order:
- All synchronous: 1, 4
- All microtasks (Promises): 3
- Then macrotasks (timers): 2
17. Promise vs Timeout
setTimeout(() => console.log("A"));
Promise.resolve().then(() => console.log("B"));Output:
B
AWhy: Promises (microtasks) always run before timers (macrotasks) on the next tick.
18. async + await null
async function test() {
console.log(1);
await null;
console.log(2);
}
test();
console.log(3);Output:
1
3
2Why: await splits execution. console.log(1) runs, then control returns to the main stack, logging 3. The continuation (console.log(2)) is queued as a microtask.
19. Microtask Scheduling Timer
Promise.resolve().then(() => {
setTimeout(() => console.log("X"));
});
Promise.resolve().then(() => console.log("Y"));Output:
Y
XWhy: First microtask schedules a timeout; second microtask logs "Y". Only after microtasks finish does the timeout log "X".
20. Classic Event Loop Example
console.log("start");
setTimeout(() => console.log("timeout"));
Promise.resolve().then(() => console.log("promise"));
console.log("end");Output:
start
end
promise
timeoutWhy: Script: "start", "end". Then microtask "promise". Then timeout "timeout".
21. Promise vs queueMicrotask vs Timeout
setTimeout(() => console.log(1), 0);
Promise.resolve().then(() => console.log(2));
queueMicrotask(() => console.log(3))Output:
2
3
1Why: Both then and queueMicrotask schedule microtasks. They run before timers, and in the order they were queued: Promise microtask then explicit microtask.
22. await Another async Function
async function a() {
console.log(1);
await b();
console.log(2);
}
async function b() {
console.log(3);
}
a();
console.log(4);Output:
1
3
4
2Why: a() logs 1, calls b() which logs 3, then await pauses a. Control returns and logs 4. Later, continuation of a logs 2 as a microtask.
23. Promise Now, Promise in Timeout
Promise.resolve().then(() => console.log("P"));
setTimeout(() => Promise.resolve().then(() => console.log("M")));Output:
P
MWhy: First microtask logs "P". Timeout callback runs in the future, inside it a new microtask logs "M".
24. queueMicrotask vs Promise vs Sync
queueMicrotask(() => console.log(1));
Promise.resolve().then(() => console.log(2));
console.log(3);Output:
3
1
2Why: Sync 3 first, then microtasks FIFO: 1, then 2.
25. Two Timers, One Promise
setTimeout(() => console.log("T1"));
setTimeout(() => console.log("T2"));
Promise.resolve().then(() => console.log("P"));Output:
P
T1
T2Why: Microtask "P" runs before any timer callbacks. Timers fire later in scheduling order.
26. async Function and await Promise.resolve()
async function run() {
console.log("A");
await Promise.resolve();
console.log("B");
}
run();
console.log("C");Output:
A
C
BWhy:await defers the rest of run to a microtask. "A" and then "C" log synchronously, "B" later.
27. Two Promise .thens
Promise.resolve().then(() => console.log(1));
Promise.resolve().then(() => console.log(2));Output:
1
2Why: Microtasks preserve registration order.
28. Two Timers
setTimeout(() => console.log(1));
setTimeout(() => console.log(2));Output:
1
2Why: Both timers have same delay; callbacks fire in scheduling order.
29. Microtask Queuing Microtask
Promise.resolve().then(() => {
console.log("A");
Promise.resolve().then(() => console.log("B"));
});Output:
A
BWhy: First microtask logs "A" and queues another microtask, which runs immediately after, logging "B".
30. Top-Level await in Script
console.log("X");
await Promise.resolve();
console.log("Y");Output:
SyntaxErrorWhy: Top-level await is only allowed in modules, not in classic script context.




