How to Make Array Iterations With Async/Await Functions in Node.Js

How to run synchronous loops in the Node environment

Farrukh Atabekov
The Startup
4 min readJul 25, 2020

--

PS: Did you get disappointed when the code you are writing in Node was not giving you expected results or when you get different outputs than the ones you would get by implementing the same logic in a different language. Well, Node runs on asynchronous environment and I will show you how to make some of those codes run synchronously.

Asynchronous nature of Node is one of its performance boosters but this asynchronous behavior can sometimes be a performance issue too.

Let’s say you want to make an asynchronous call to API and you want to do it in some specific order. You might have an ordered array to pull the data from API or you might want to store the API response in a specific order. In those cases, using built in array methods throw an unexpected result.

Let me show you an example.

I will be using the http://jsonplaceholder.typicode.com/posts as a sample and I will be pulling data using a popular library called ‘axios’.

So you want to make an API call to get the posts from 1 to 10. This is the asynchronous function to pull that data from API.

Function to make an API call

This function takes the post number if (provided), makes a request to the API and returns data if there is no error.

Asynchronous functions are commonly used to pull data from API because since you are pulling that data from an outside provider, you have to wait until that response received and only after you receive that response, you can get started working with that data.

So in order to get the data for posts from 1 to 10, we could write another function. One of the options that comes to mind to perform this operation is the Array.forEach().

Array iterated async function calls

After running those functions together like this, it is the output that I get.

Asynchronous result order

As you can see, the outputs order does not match with the order in the array. In fact, even the output from the console.log() function is printed on the top rather than on the bottom.

So why is this happening and how we can fix this?

First of all , this is happening because of the unblocking nature of Node.js. When we run the getPostsAsync() function, it fires all those functions inside and since asynchronous requests to the API takes some time, it consoles the response from console.log() first and after that which ever response is received will be printed to the console.

Second of all, to avoid this issue, we can use a couple of other methods. One of them is the typical for loop. ‘For loop’ waits for the first iteration to finish before proceeding to the next task.

We get the output below.

We get the same ordered result using the while loops too.

Using those traditional loops not only returns the data in order but they also return the response faster than Array.forEach() method. So this is how we can run array iterations in Node synchronously.

Advantages of Asynchronous functions

Another point in case, even if it is a drawback in this case, this asynchronous nature is one of the performance boosters in Node.js because of the fact that it allows you to perform other tasks while waiting for the response from API. Other languages such as Python waits until the response is received and only after that, it proceeds to perform the next task. It is not a good feature because CPU is left idle while waiting for the response, thus wasting time. Node avoids waiting for the response and keeps proceeding for next tasks.

Thanks for reading and I hope it helps you to better understand array iterations in Node.js

--

--