Promise.all API 调用,区分哪个抛出错误并只拒绝一个
Promise.all API calls, distinguish which threw error and reject only one
所需信息:使用 NodeJS 框架,Promises.all 与 API 一起使用仅调用异步代码
所以我的问题的根源在于我需要创建两个 API 调用,我们称它们为 A 和 B。保证 A 将 return 数据或 404 和 B将 return 一个空数组、数据或 404(但这里的 404 表示无效输入,而在 API 调用 A 中,它实际上表示未找到资源)。我的问题是,如果 A 执行 return 404,Promise.all 将像往常一样拒绝并跳入 catch 块。
我想要的功能是,如果 API 调用 A return 一个 404,我希望 API 调用 B 继续并检索该数据,继续我的代码。有没有办法区分甚至分别捕获两个 API 调用抛出的错误,然后在一个解决后继续??
示例代码目前如下所示:
function(param) {
const A = apiCallA();
const B = apiCallB();
return Promise.all([A, B])
.then( ([resA, resB]) => {
// If resA is null, catch the error but continue with
// resB. If this is null also Promise.reject('both API calls failed')
// else if both resA && resB != null, do some stuff and resolve
})
.catch( (err) => {
// Here is where my question lies. The err object can be either from API call A or B.
// How would I distinguish this?
});
}
有几个选项。
您可以在 Promise.all()
之前从 API 调用 A 中捕获错误并将其转换为成功的请求但适当标记,从而允许 Promise.all()
完成.
你可以用Promise.allSettled()
得到两种结果,不管成功还是失败。
对于第一个选项,您可以在 apiCallA()
上放置一个 .catch()
处理程序,它将把任何拒绝转化为解决,但将使用错误对象解决,您稍后可以检查您是否需要:
function(param) {
const A = apiCallA().catch(err => { return {err} });
const B = apiCallB();
return Promise.all([A, B]).then( ([resA, resB]) => {
// you can check if resA succeeded here
if (resA instanceof Error) {
// resA actually failed
console.log(resA);
}
}).catch( (err) => {
// you would only get here if resB failed
});
}
对于第二个选项,您使用 Promise.allSettled()
:
function(param) {
const A = apiCallA();
const B = apiCallB();
return Promise.allSettled([A, B]).then( ([resA, resB]) => {
// check both resA.status and resB.status
if (resA.status === "fulfilled") {
console.log(resA.value);
}
if (res === "fulfilled") {
console.log(resB.value);
}
});
}
所需信息:使用 NodeJS 框架,Promises.all 与 API 一起使用仅调用异步代码
所以我的问题的根源在于我需要创建两个 API 调用,我们称它们为 A 和 B。保证 A 将 return 数据或 404 和 B将 return 一个空数组、数据或 404(但这里的 404 表示无效输入,而在 API 调用 A 中,它实际上表示未找到资源)。我的问题是,如果 A 执行 return 404,Promise.all 将像往常一样拒绝并跳入 catch 块。
我想要的功能是,如果 API 调用 A return 一个 404,我希望 API 调用 B 继续并检索该数据,继续我的代码。有没有办法区分甚至分别捕获两个 API 调用抛出的错误,然后在一个解决后继续??
示例代码目前如下所示:
function(param) {
const A = apiCallA();
const B = apiCallB();
return Promise.all([A, B])
.then( ([resA, resB]) => {
// If resA is null, catch the error but continue with
// resB. If this is null also Promise.reject('both API calls failed')
// else if both resA && resB != null, do some stuff and resolve
})
.catch( (err) => {
// Here is where my question lies. The err object can be either from API call A or B.
// How would I distinguish this?
});
}
有几个选项。
您可以在
Promise.all()
之前从 API 调用 A 中捕获错误并将其转换为成功的请求但适当标记,从而允许Promise.all()
完成.你可以用
Promise.allSettled()
得到两种结果,不管成功还是失败。
对于第一个选项,您可以在 apiCallA()
上放置一个 .catch()
处理程序,它将把任何拒绝转化为解决,但将使用错误对象解决,您稍后可以检查您是否需要:
function(param) {
const A = apiCallA().catch(err => { return {err} });
const B = apiCallB();
return Promise.all([A, B]).then( ([resA, resB]) => {
// you can check if resA succeeded here
if (resA instanceof Error) {
// resA actually failed
console.log(resA);
}
}).catch( (err) => {
// you would only get here if resB failed
});
}
对于第二个选项,您使用 Promise.allSettled()
:
function(param) {
const A = apiCallA();
const B = apiCallB();
return Promise.allSettled([A, B]).then( ([resA, resB]) => {
// check both resA.status and resB.status
if (resA.status === "fulfilled") {
console.log(resA.value);
}
if (res === "fulfilled") {
console.log(resB.value);
}
});
}