Why JavaScript doesn't just wait around — and how that changes everything about how you write code.
Let me tell you about the moment asynchronous JavaScript broke my brain.
I wrote this code, fully expecting it to print in order:
console . log ( " First " ); setTimeout (() => console . log ( " Second " ), 0 ); console . log ( " Third " );
Enter fullscreen mode
Exit fullscreen mode
I expected: First → Second → Third.
I got: First → Third → Second.
Zero milliseconds of delay, and "Second" still printed last. I stared at my screen thinking JavaScript was broken. It wasn't. I just didn't understand how JavaScript handles time.
This is the story of synchronous vs asynchronous code, and once you get it, a massive amount of JavaScript behavior suddenly makes sense.…