Skip to content

Commit 7f6b8aa

Browse files
committed
Improve README.md: formatting, clarity, and structure
1 parent dc7db5b commit 7f6b8aa

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12520,6 +12520,37 @@ This question is really showcasing how JavaScript mixes array reduction with low
1252012520
</p>
1252112521
</details>
1252212522
12523+
### 88.What is the output of the following snippet?
12524+
***javascript
12525+
console.log("A");
12526+
12527+
setTimeout(() => {
12528+
console.log("B");
12529+
}, 0);
12530+
12531+
Promise.resolve().then(() => {
12532+
console.log("C");
12533+
});
12534+
12535+
console.log("D");
12536+
12537+
***
12538+
12539+
- 1. A D B C
12540+
- 2. A D C B
12541+
- 3. A C D B
12542+
- 4. A B C D
12543+
12544+
<details><summary><b>Answer</b></summary>
12545+
<p>
12546+
### Answer : 2
12547+
12548+
The output is A D C B because JavaScript executes all synchronous code first, so “A” and “D” are printed immediately. After that, the event loop handles asynchronous tasks, where Promise.then() is placed in the microtask queue and setTimeout(...,0) goes to the macrotask queue. Since microtasks always execute before macrotasks—regardless of the timeout value—the promise callback prints “C” next, and finally the timeout callback prints “B”, resulting in the final order: A D C B.
12549+
12550+
</p>
12551+
</details>
12552+
12553+
1252312554
**[⬆ Back to Top](#table-of-contents)**
1252412555
1252512556
## Disclaimer

0 commit comments

Comments
 (0)