我必须进行一些api调用来获取数据以生成视图。可以说有两个api调用,API1和API2。必须解析API1才能生成最小视图。如果API2也得到解决,我可以显示其他功能。
我想同时进行两个调用,并等待API1和API2解析或拒绝使用

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


但是,正如您所看到的,这里我只介绍了两种情况,而不是我想要的一种情况。即使API2失败,我也必须解决它。怎么做??

最佳答案

您可以执行以下操作:

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
});

关于javascript - 使用promise进行多个异步调用,其中只有很少几个真正重要,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39623044/

10-12 17:22