The significance of async

At some point, one of your Javascript programs will need to interact with the outside world - by shaking hands with another system, fetching content, or sending data somewhere.

Being a single-threaded programming language, a Javascript program needs a way to interact with the outside world without holding everything up. To achieve this, it uses asynchronous operations, which are used in both the traditional client-side Javascript, as well as in server-side Javascript programs, such as those running on Node JS.

Trying out asynchronous requests

A helpful introduction to async requests in Javascript is available at w3schools. A good way to get started is by trying a setTimeout function, which is by nature asynchronous. A more complex async request can be tried with client-side Javascript using the XMLHttpRequest operation.

Recognizing async requests

When learning about async functions, it can be helpful to understand them in Javascript and recognize where they show up.

Callbacks

One of the earliest ways of handling async requests in Javascript has been with callbacks.

While the request-response sequence of asynchronous requests happen outside of Javascript’s thread, the program needs to be ready to do something once a response is received. To handle these responses, Javascript uses callbacks, or functions that kick off once a response to an async request is received. Usually the callback will take the data produced by the response and do something with it.

A helpful introduction to callbacks is available on this w3Schools tutorial. I recommend trying these particular examples with “vanilla Javascript” on the client-side without Node JS.

That said, callbacks are also used in Node JS, and are discussed in this post on NodeJS.org.

Promises and Async / Await

Async requests are are also handled by Promise objects, which are described in detail by Eric Elliot in this terrific post.

Promise objects still require callbacks, but in a different way.

The relatively new async / await syntax, added to Javascript in 2017, is also designed to effectively handle asynchronous requests with a syntactically straightforward approach. Async / await is used both in client-side Javascript and in Node JS, as described in this Rising Stack post by Tamas Kadlecsik and János Kubisch.

I use async / await requests in my apps for writing to and reading from the file system, and am excited to be able to do this with Node JS.