Skip to main content

A.P.I call or async await not working in Array forEach ?

Hello Readers, Welcome back.


You would have wondered why does forEach function on the array does not wait for any asynchronous function even if we provide the async await in the node js. If you are the person, wondered about this ever, this post is right for you.


Non working example :


Lets consider the below snippet, this will not wait for the asynchronous process to wait. I am making a setTimeout to mock the API call async process.


This will result the count as,

Output : count = 0

OMG !! Why it doesn't work ?

Answer probably might be lying inside the Array prototype from JavaScript. Lets take a look inside the "Array.prototype.forEach" function.


From the snippet its clear that, the for loop in which they call the callback function does not wait for the asynchronous process to run in the callback. So this forEach is not build for asynchronous process itself.

So can't I use forEach any more for running a asynchronous function ?

Answer goes like this, "Yes, there are various methods to iterate the array and achieve the asynchronous call" Lets see them one by one below.



Method 1 :


Building a wrapper around the forEach:


As you can see, we made a immediate calling function that return the Promise and we wait for that using the await. To end the wait, we call the resolve in the if condition (index === array.length -1) means the last iteration of the loop.

The same method can be extended using the Array.prototype.forEachAsync to build a wrapper for the forEach as below,


Post creating this in a file called "Array.prototype.forEachAsync.js", include that in the files where you need to use this function, or you can include this globally into the node js.

Now this becomes same as that of using the forEach but with the async support



Method 2 :


Now lets make use of the Promise.all and map of the array to make the same get achieved.




Method 3 :


In similar way, without using any functions, we can even make use of traditional for loop and for in  loop


Its always good that we should dig deep into the architecture of the underlying API to create solutions of our own. Hope this post solved the issue of async in the forEachLoop. Thanks readers, keep following and stay tuned !




Comments

Popular posts from this blog

How Machine Learning Works ? - Data Rules the Kingdom

W elcome Back Readers !, For those who have not read the previous post on How Machine Learning works ? Please do read previous one before continuing this so that it would be more easy to understand. You can find the previous post here How Machine Learning Works ? - Mathematics Every Where . For other who are ready to join with me in this journey to understand the Machine Learning, Thank you for being with this post. Till now we are discussing on the the title "How Machine Learning works? " and to understand that we have seen in the first post that there exist the patterns or trends in the data and we try to find that in terms of a mathematical equation called a model and try to fit to the new data that we have not came across for predicting the values. Wow may be we got the entire process complete with this. But there are few thinks that needs to be cared while training the model or creating the model or while processing the data for the model creation. ...

Complete On-Premise and Fully Customisable Chat Bot - Part 4 - Integrating the Natural Language Processor NLP

Welcome back Folks !! In our part 3 we have seen how to communicate to the bot that we have built. Each and every people would talk the same thing in the different format and on his style. For each response if we start writing the code, Imagine how hard its gonna be. What can we do for that ? Natural Language Processor makes it easy to do. NLP is a component that identifies the user conversation and identifies the meaningful context from that. What NLP is available ? There are few NLP services like, 1. Dialog flow 2. Luie 3. IBM Voice etc. But all these are the cloud services and we need to go internet for this. As we speaking about the on premise chat bot we will go for the RASA solution. What is RASA ? RASA is a Chatbot server and RASA NLU is the NLP server that comes handy and is built on the python tech stack. Its is a open source and backed by a strong community. Lets get started with RASA ... 1. Installing the python environment :  Si...

Machine Learning - Types of Learning - Explained

W elcome back readers !! So far we are trying to understand the Machine learning in a simple way, keeping the beginners context in mind ! This is one more post, I thought I would write on explaining the various learning types in the Machine Learning. Obviously the word says "Machine Learning" - means machine learn something. How do machine can learn something is what we understood in our various posts before ( here ). We will see about how many types of learning are possible by the machines. Here when I say types of learning, I say about the various classification of learning procedures (Algorithm - Step by step procedure to solve a problem). Types of Learning: Following types of learning procedures can be identified, Regression Classification Clustering Rule association  Ensemble  Reinforcement Deep Learning I can understand that this sounds Greek and Latin now, trust me I will make you understand all this in easy way. We can't cover all the types...