Skip to main content

Call async function in loop for array and update the exiting array Javascript and Node Js

Here is a good example of the code to call async function in loop for element of an array and update the exiting array. In this example I have used the Array.prototype.entries() function to get the index and value of each array item of a simple array. Array.prototype.entries() method helps to iterate an array with index and element.

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function resolveAfter2Seconds(num) {
    await timeout(2000);
    return num;
}


async function main() {
    let arr = ["Saab", "Volvo", "BMW"];
    
    for (let [index, val] of arr.entries()) {
        console.log(index, val);
        console.log(await resolveAfter2Seconds(val));
    }
}

main();

Comments

Popular posts from this blog

The Great Architectures of Bijawar, Chhatarpur, M.P.

Bijawar, Temple Janki Niwash Bijawar, Temple Janki Niwash   Maharaja Palace of Bijawar - A very royal palace of the Great Maharaja Ji of Bijawar. The palace has a great eye caching architecture. It stands tall with its mirror image on a big Talab in front of it. The palace is become very old and loosing its age at a rapid pace. Unfortunately its to late to restore the Bijawar palace infrastructure but one should create Bijawar palace replica or model to preserve its beauty and architecture. This photo of the palace is the first photo taken and publish on the web. That time there was no good cameras available but as said Old is Gold.

You are running 'create-react-app' 4.0.3 which is behind the latest release (5.0.1).

I was trying to create a new React app with the following command. > npx create-react-app react-app Then I get the following error: You are running `create-react-app` 4.0.3, which is behind the latest release (5.0.1). We no longer support global installation of Create React App. I also get some help instructions as well as: Please remove any global installs with one of the following commands: - npm uninstall -g create-react-app - yarn global remove create-react-app I tried to run the floowing command multiple times as suggested: > npm uninstall -g create-react-app Which ran fine with no error that means my create-react-app glabal installation uninstalled successfully. Then I again tried the same command to install the React app: > npx create-react-app react-app But I again get the same errors and suggestions. I repeated the same procedure multiple times but it did not work. Then I decided to look for the solution over a search engine. By no much time, I got some useful help th