This book so far has been all about how to leverage asynchrony patterns more effectively. But we haven’t directly addressed why asynchrony really matters to JS. The most obvious explicit reason is performance.
For example, if you have two Ajax requests to make, and they’re independent, but you need to wait on them both to finish before doing the next task, you have two options for modeling that interaction: serial and concurrent.
You could make the first request and wait to start the second request until the first finishes. Or, as we’ve seen both with promises and generators, you could make both requests in parallel, and ask the gate to wait on both of them before moving on.
Clearly, the latter is usually going to be more performant than the former. And better performance generally leads to better user experience.
It’s even possible that asynchrony (interleaved concurrency) can improve just the perception of performance, even if the overall program still takes the same amount of time to complete. User perception of performance is every bit—if not more!—as important as actual measurable performance.
We want to now move beyond localized asynchrony patterns to talk about some bigger picture performance details at the program level.
You may be wondering about micro-performance issues, like if
++a is faster. We’ll look at those sorts of performance
details in Chapter 6.
If you have processing-intensive tasks but you don’t want ...