-1

I have multiple GET API requests i want to do in a Loop. What is the best way to do this using await in a try/catch.

I have something like this currently:

requestItems.forEach(url => {
        
        try {
          data = await apiService.get(url);
        } catch (err) {
          console.log(err)
        }
    });

1 Answer 1

2

To run it in parallel you can do something like this:

const requestAll = async () => {  
  return await Promise.all(requestItems.map(async url => {
    try {
      return await apiService.get(url);
    } catch (err) {
      console.log(err)
    }
  }));
}
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.