As a practical concern, we want our programs to be performant. Despite our focus in this book being interfaces rather than performance, there is another issue, even if we thought it was okay to hold up our whole program for a web request or data processing task that could take seconds, minutes, or even longer: sometimes async is the only option for a given module or library.
Async has become the norm for many APIs. For example, one coming from a mostly synchronous paradigm (language or style) might expect node’s
http module to behave like this:
But this will print
undefined. The reason is that our
response constant is actually named a bit optimistically. The return value of
http.get is a
ClientRequest object, not a response at all. It describes the request, but not the result.
There are good reasons to use asynchronous remote HTTP calls. If our remote call were synchronous, it would necessarily “stop the world” (STW) and keep ...