Home > Software design >  how to call multiple URLs within array at same time using node js
how to call multiple URLs within array at same time using node js

Time:11-22

When I have different types of URLs in a single array of strings, in my JSON file, Now I use the map function to post data via apiurl

an array of URLs:

 "ResponseServer": [
        "https://ResponseServer1/home/",
        "https://ResponseServer2/home/",
        "https://ResponseServer3/home/"
    ],

Map function

     Responseserver.map((Resposnserver) => {
            axios.post(Resposnserver, data)
}}

every time the function reads each array of URLs, but I wish to call all APIs at the same time.

CodePudding user response:

You should use Promise.all

const responses = await Promise.all(Responseserver.map((url) => {
  return axios.post(url, data)
})

Promise all will throw an error if at least one API call fails, you can use Promise.allSettled if you need to fulfill all the API calls despite any rejections.

  • Related