0

I have a promise which has child promise in it. when I am running the child promise which has array of urls, and mapping through each of urls to get the response but its not waiting for request to complete and moves to next iteration in parent promise.

Calling fetchProductByUrl from parent promise

let imageUrl = [primaryImage ,  ...productImages];

    console.log("starting of fetching images");

    let images = await fetchProductByUrl(imageUrl ,"image");
async function fetchProductByUrl(url_array , cfmModelName) {

  try {

       let promiseArray =  await Promise.all(url_array.map(async (url) => {

          return  axios.get(url); // this is completing after all parent's iteration

        })).then(res => {

          return res

        })

  } catch (error) {

    console.log(error);

    return error;

  }

}

Is there anything wrong in the fetchProductByUrl function. It doesnt wait for all the request and move to the next iteration in parent promise.

Tried with async and await and also axios.all. Same issue. Expecting child promise fetchProductByUrl to get all response from all array of urls and then move to next iteration in parent promise

  • *"...but its not waiting for request to complete and moves to next iteration in parent promise."* Why would it? `map` is a synchronous operation. Do you really want to do all of the `axios.get` calls in *series* rather than in parallel? Unless you have a specific reason for that, it seems like it would be better to make all the requests and then wait for them all to complete (as you're doing now). – T.J. Crowder Apr 19 '23 at 09:49
  • [My answer here](https://stackoverflow.com/a/43766002/157247) describes doing things like this in series or in parallel. – T.J. Crowder Apr 19 '23 at 09:50
  • 1
    (Side note: There's never any point to `.then((res) => { return res; })`. It doesn't do anything.) – T.J. Crowder Apr 19 '23 at 09:54
  • there is only one array, is it an array of arrays ? or just array of urls ? – Rituparna Warwatkar Apr 19 '23 at 10:27
  • @T.J.Crowder I want the axios.get to run in parallel and complete. My parent promise is also an array of promises. Its not completing the child promise for one parent and moving to the next iteration in parent. How can i have it wait for the child promise to complete and then move to next parent promise – Akshay kumar Apr 19 '23 at 10:54

0 Answers0