
Async Await Is Just a Bookmark
https://www.youtube.com/watch?v=2ulRI-a5ea0 Here's what most developers think async/await does: you call an async function, and it runs on a separate thread. Maybe a background worker. Something parallel. Wrong. Async/await does not create threads. It does not run things in parallel. One thread. One function at a time. Always. So how does your app handle a thousand network requests on a single thread? The Compiler Rewrites Your Function When you write an async function, the compiler doesn't keep it as a function. It transforms it into a state machine . Each await keyword becomes a checkpoint. State 0 runs the code before the first await. State 1 runs the code between the first and second await. State 2 picks up after that. The function remembers which state it's in and all its local variables. When execution hits an await , the function doesn't block. It doesn't spin. It saves its state and returns control to the caller. The function literally pauses itself. The Bookmark Metaphor Think
Continue reading on Dev.to
Opens in a new tab

