如何跳过导致异常的链式承诺?
How can I skip chained promises resulting in exceptions?
我有多个 api 要命中。
例
callApis = async function () {
try {
var apis = ["apiWithSucess1", "apiWithException", "apiWithSucess1"];
var response = "";
for(var i = 0; i < apis.length; i++){
const apiResponse = await httpRequest.get(apis[i]).promise();
response+=apiResponse.data;
}
return response;
}
catch (err) {
console.log("Exception => ", err);
}
};
callApis().then(function(result){
console.dir(result);
}).catch(function(err) {
console.log(err);
});
现在,当我调用它时,如果数组中有一些 api 抛出异常,它会使所有进程崩溃。我希望 api 被跳过。
插入 try
/catch
子句:
...
let apiResponse
for(var i = 0; i < apis.length; i++){
try {
apiResponse = await httpRequest.get(apis[i]).promise();
catch (error) {
console.error(error)
continue
}
response+=apiResponse.data;
}
...
try
子句中的任何内容通常都会 运行,除非抛出 exception/error。在那种情况下,它最终出现在 catch
子句中,可以在其中处理该问题。我只是在那里放置了一个 continue
语句,所以你只会得到好的响应,尽管你也可以在响应中添加一个 null
,然后是 continue
,这样你的响应数组就有序了.
如果使用 .promise().catch(() => "")
).
忽略错误,则可以像 , but if it is not important that the APIs are chained or otherwise called in sequence, then you have the opportunity to call them in parallel to make the process faster. This would entail calling Promise.allSettled()
(or Promise.all()
一样使用 try
/catch
捕获错误
在常规语法中:
callApis = /* no longer async */ function () {
var apis = ["apiWithSuccess1", "apiWithException", "apiWithSuccess1"];
// For each API, call promise() and mute errors with catch().
// Call Promise.all to wait for all results in parallel.
return Promise.all(apis.map(x => httpRequest.get(x).promise().catch(() => "")))
// Join the results array as a string with no separators.
.then(arrayOfStrings => arrayOfStrings.join(""))
// If any of the above steps fails, log to console and return undefined.
.catch(err => { console.log("Exception => ", err); });
}
在您的 async
/await
语法中,除了 map
:
中的异常静音 catch
调用
callApis = async function () {
try {
var apis = ["apiWithSuccess1", "apiWithException", "apiWithSuccess1"];
// For each API, call promise() and mute errors with catch().
// Call Promise.all to wait for all results in parallel.
var responseArray = await Promise.all(apis.map(
x => httpRequest.get(x).promise().catch(() => "")));
// Join the results array as a string with no separators.
return responseArray.join(""));
}
catch (err) {
console.log("Exception => ", err);
}
}
我有多个 api 要命中。
例
callApis = async function () {
try {
var apis = ["apiWithSucess1", "apiWithException", "apiWithSucess1"];
var response = "";
for(var i = 0; i < apis.length; i++){
const apiResponse = await httpRequest.get(apis[i]).promise();
response+=apiResponse.data;
}
return response;
}
catch (err) {
console.log("Exception => ", err);
}
};
callApis().then(function(result){
console.dir(result);
}).catch(function(err) {
console.log(err);
});
现在,当我调用它时,如果数组中有一些 api 抛出异常,它会使所有进程崩溃。我希望 api 被跳过。
插入 try
/catch
子句:
...
let apiResponse
for(var i = 0; i < apis.length; i++){
try {
apiResponse = await httpRequest.get(apis[i]).promise();
catch (error) {
console.error(error)
continue
}
response+=apiResponse.data;
}
...
try
子句中的任何内容通常都会 运行,除非抛出 exception/error。在那种情况下,它最终出现在 catch
子句中,可以在其中处理该问题。我只是在那里放置了一个 continue
语句,所以你只会得到好的响应,尽管你也可以在响应中添加一个 null
,然后是 continue
,这样你的响应数组就有序了.
如果使用 .promise().catch(() => "")
).
Promise.allSettled()
(or Promise.all()
一样使用 try
/catch
捕获错误
在常规语法中:
callApis = /* no longer async */ function () {
var apis = ["apiWithSuccess1", "apiWithException", "apiWithSuccess1"];
// For each API, call promise() and mute errors with catch().
// Call Promise.all to wait for all results in parallel.
return Promise.all(apis.map(x => httpRequest.get(x).promise().catch(() => "")))
// Join the results array as a string with no separators.
.then(arrayOfStrings => arrayOfStrings.join(""))
// If any of the above steps fails, log to console and return undefined.
.catch(err => { console.log("Exception => ", err); });
}
在您的 async
/await
语法中,除了 map
:
catch
调用
callApis = async function () {
try {
var apis = ["apiWithSuccess1", "apiWithException", "apiWithSuccess1"];
// For each API, call promise() and mute errors with catch().
// Call Promise.all to wait for all results in parallel.
var responseArray = await Promise.all(apis.map(
x => httpRequest.get(x).promise().catch(() => "")));
// Join the results array as a string with no separators.
return responseArray.join(""));
}
catch (err) {
console.log("Exception => ", err);
}
}