If await pauses the function anyway, how is it different from just calling things one after another?
I am struggling with what await actually buys me. Consider two versions of the same function: one calls three asynchronous operations with await on each line, the other is a hypothetical version where the same operations are synchronous and blocking.
In both cases the operations happen in order, each finishes before the next begins, and the function does not return until all three are done. From inside the function they look identical.
So what is the difference, and where does the benefit actually appear? I understand the syntax and not the point.
@api_dilan · 21h ago
There is a second, practical difference that shows up in your own code rather than in the runtime, and it is where most real performance wins come from.
Because the operations are values rather than blocking statements, you can choose to not sequence them:
If those three calls are independent, the second version finishes in the time of the slowest rather than the sum of all three. That option simply does not exist with blocking calls.
The common mistake, once people learn
await, is writing the first version everywhere out of habit. Whenever you see several awaits in a row, ask whether any of them actually depend on the ones above.Reply
Report