javascript axios.all 传播并捕获所有

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48681126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:11:39  来源:igfitidea点击:

axios.all spread and catch all

javascriptajaxaxios

提问by WebArtisan

I'm using .all method of popular library 'axios' for handling my ajax requests.

我正在使用流行库“axios”的 .all 方法来处理我的 ajax 请求。

But how can I handle errors in case all requests got 404?

但是,如果所有请求都得到 404,我该如何处理错误?

for example:

例如:

axios.all([
        axios.get('http://some_url'),
        axios.get('http://another_url'),
      ])
        .then(axios.spread((someUrl, anotherUrl) => {
         // ... boring stuff goes there
        }))
        .catch(() => {
         //... error goes there
        });

enter image description here

在此处输入图片说明

So, seems only one error has ben "catched".

因此,似乎只有一个错误被“捕获”了。

How can I catch them all? Or maybe there any kinda .finally?

我怎样才能抓住他们?或者也许有什么.finally?

回答by dfsq

The problem (as you already know) is that you will get into catchblock as soon as the first promise rejects, making it impossible to collect all failed responses in the same catch. However, you still can handle failed promises manually to aggregate errors and throw afterwards.

问题(正如您已经知道的)是,catch一旦第一个承诺被拒绝,您就会进入阻塞,从而无法在同一个catch. 但是,您仍然可以手动处理失败的承诺以聚合错误并在之后抛出。

Check it this will work for you:

检查它这对你有用:

const promises = [
  axios.get('http://some_url'),
  axios.get('http://another_url'),
]
const promisesResolved = promises.map(promise => promise.catch(error => ({ error })))

function checkFailed (then) {
  return function (responses) {
    const someFailed = responses.some(response => response.error)

    if (someFailed) {
      throw responses
    }

    return then(responses)
  }
}

axios.all(promisesResolved)
  .then(checkFailed(([someUrl, anotherUrl]) => {
    console.log('SUCCESS', someUrl, anotherUrl)
  }))
  .catch((err) => {
    console.log('FAIL', err)
  });

You will get into catchblock if at least one of the promises fails. You can find one which one by checking errarray of responses.

catch如果至少有一项承诺失败,您将进入阻塞状态。您可以通过检查err响应数组找到其中一个。

回答by Charlie

I don't think this is possible due to the fail fast behaviour of Promise.all. If any of your requests fail, they will automatically be the culprit and the result in the catch.

由于 Promise.all 的快速失败行为,我认为这是不可能的。如果您的任何请求失败,它们将自动成为罪魁祸首,并成为捕获的结果。

    Promise.all([
      Promise.reject(Error('1')),
      Promise.reject(Error('2')),
      Promise.reject(Error('3'))
    ]).then((results) => {
      console.log(results)
    }, (error) => {
      console.log(error.message)
    })

This resulting code will always print 1 as it is the first to fail.I think a similar feature was requested on the repo and they said it wasn't possible.

生成的代码将始终打印 1,因为它是第一个失败的。我认为 repo 上要求了类似的功能,他们说这是不可能的。

I was going to leave this as a comment but don't have a high enough reputation yet.

我打算留下这个作为评论,但还没有足够高的声誉。