Skip to content
On this page

Asynchronous Programming

Javascript is single threaded. Avoid using operations that block while they complete so the thread is free to do other work.

The language has created a few different patterns to handle this situation.

Originally there were callback functions... a function to call once the work was complete.

Now there are Promises.

async and await is syntatic sugar to make using Promises easier. They create Promises behind the scenes.

For more about this evolution... The Evolution of Asynchronous JavaScript | RisingStack

forEach loops

Before we even get into other details... IMPORTANT:

async/await does not work well with standard forEach loops. If you expect the loop to wait for the internal await commands, you'll be surprised to see things happening out of order.

I feel like this issue trips me up frequently:
JavaScript: async/await with forEach() | by Sebastien Chopin | codeburst

The solution is to make an asynch/await compatible forEach function:

async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);


It's also possible to group promises and wait for them all to complete
javascript promises all at DuckDuckGo
Promise.all() - JavaScript | MDN

Async / Await

async function hello() { return "Hello" };

The async keyword is added to functions to tell them to return a promise rather than directly returning the value.

Then, in an async function, you can use await to ensure subsequent actions don't happen until after await completes


Modern libraries return promises. Even with async/await being available, it's useful to understand what is happening behind the scenes.

Promises are a way to chain actions/calls/functions without using callbacks.

As long as each .then() returns a promise, you can chain multiple .then steps and finish with a single .catch

const axios = require(‘axios’);
.then(function (response) { // Reponse being the result of the first request
    // Returns another promise to the next .then(..) in the chain
    return axios.get(`${response.someValue}`);
.then(function response { // Reponse being the result of the second request
    // Handle response
.catch(function (error) {
    // Handle error.

Arrow Functions

Arrow functions allow access to the local (containing parent) scope when writing functions.

For some reason, I always associate arrow functions with promises and asynchronous coding styles in javascript.

"Two factors influenced the introduction of arrow functions: the need for shorter functions and the behavior of the this keyword."

"An arrow function does not have its own this. The this value of the enclosing lexical scope is used; arrow functions follow the normal variable lookup rules. So while searching for this which is not present in current scope, an arrow function ends up finding the this from its enclosing scope."

Arrow function expressions - JavaScript | MDN
js arrow functions - Google Search

See Also

Library to help with asynchronous patterns
Home - Documentation