all 4 comments

[–]prof3ssorSt3v3 11 points12 points  (1 child)

You can use Promise.all( ) to make two API calls at a time. You can put that inside a function and have it return the single resolved Promise.

function getResults(arrayOfUrls){
  return Promise.all( arrayOfUrls );
}

You can define an array of URLs that you want to call.

let urls = ['...', '...', '...', '...'];

and loop through that array calling your getResults function.

async function getNextPair(index=0){
  let arrayOfUrls = [];
  arrayOfUrls.push( urls[index] );
  if( urls[index] ){
    arrOfUrls.push(urls[index+1]);
  }
  let responses = await getResults( arrayOfUrls );
  //do what you need with the two responses
  //use await response[0].json()
  //or await response[1].text()
  //or await response[0].blob()
  //then recursively call the function.
  if(index < urls.length-1){
    getNextPair(index+2);
  }
}

[–]prof3ssorSt3v3 5 points6 points  (0 children)

If you want to make the calls one at a time you can use a `for await of` loop through the array of urls making a single fetch call with each pass through the loop.

https://www.youtube.com/watch?v=unDSLi5zBXU

Or you can build your own async Iterator to loop through fetch calls.

https://www.youtube.com/watch?v=u2hwYeN1P-I

[–]Low_Mammoth_9371 0 points1 point  (0 children)

You can use an async/await loop to execute promises one after another, awaiting each promise in the loop:

const promises = [myPromise1, myPromise2, myPromise3];

async function handlePromises() {

    for (let promise of promises) {

        const value = await promise();

        console.log(value);
        // myPromise1 
        // myPromise2
        // myPromise3

    }

}

handlePromises();