DrEarnest
DrEarnest

Reputation: 885

Using promise to make multiple asynchronous calls, where only few are really important

I have to make few api calls to fetch data to generate a view. Lets say there are two api calls to be made, API1 and API2. API1 must get resolved in order to generate minimum view. If API2 also get resolved I can display extra feature. I want to make both calls simultaneously and wait for API1 and API2 to resolve or reject using

promise.all([getAPI1, getAPI2]).then(/*both successs*/).catch(/*any one fails*/)

But as you can see, I have only two scenario covered here and not the one that I want. I have to resolve it even if API2 fails. How to do that??

Upvotes: 0

Views: 39

Answers (1)

Nick Uraltsev
Nick Uraltsev

Reputation: 25907

You can do something like this:

Promise.all([
  fetchSomething(),
  fetchSomethingElse().catch(error => {
    // fetchSomethingElse failed (it's ok)
    return null;
  })
]).then(results => {
  // results[1] will be null when fetchSomethingElse fails
}).catch(error => {
  // fetchSomething failed
});

Upvotes: 1

Related Questions