1

I have two nested arrays that contain urls, for example:

[['http://ex.com/1','http://ex.com/2'],['http://ex.com/3']]

How can i fetch urls from the first array simultaneously and fetch url from the second array after resolving the first array ?

1
  • using fetch and Promise.all would be a start Commented Nov 7, 2019 at 8:05

1 Answer 1

1

You can use Promise.all() to fetch couple of urls at once.

async function getResponses (arr) {
  const res = []
  for (const urls of arr) {
    // Fetch all urls at once
    const responses = await Promise.all(urls.map(url => fetch(url)))
    res.push(await Promise.all(responses.map(response => response.text())))
  }

  return res
}

If your responses are in JSON then you can use

responses.map(response => response.json())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.