打破承诺链
Breaking a promise chain
我正在使用 axios 响应拦截器,在取消请求后,我需要打破承诺链。我不想为我的应用程序中的所有请求添加已取消请求的错误检查。我试过蓝鸟,但它似乎只是取消承诺,而不是断链。
我必须在第一次捕获时处理错误。这张图大体上显示了这个问题。 Latest then 和 catch 在不同的文件中。
Promise
.then((response) => {
)
.catch((error) => {
// break promise here
})
.then((response) => {
// skip
// I don't want any extra checks here!
)
.catch((error) => {
// skip
// I don't want any extra checks here!
})
最后只保留一个 catch 块来收集由您的承诺链触发的所有错误。
如果一个 promise 抛出链中的以下内容未被执行
Promise
.then((response) => {
)
.then((response) => {
// skip
// I don't want any extra checks here!
)
.catch((error) => {
// skip
// I don't want any extra checks here!
})
另一种选择是在最后抛出一个自定义错误,该错误可以在单个 catch
块中捕获,如下所示:
const errorHandler = require('path/to/handler')
class ProcessingError1 extends Error {
constructor(message) {
super(message);
this.name = "ProcessingError1";
}
}
class ProcessingError2 extends Error {
constructor(message) {
this.message = message;
this.name = "ProcessingError2";
}
}
const process1 = async () => {
throw new ProcessingError1("Somethign went wrong");
};
const process2 = async () => {
return { some: "process" };
};
const someApiCall = async () => ({ some: "response" });
someApiCall()
.then(process1)
.then(process2) // process2 is not run if process1 throws an error
.catch(errorHandler);
// ErrorHandler.js
module.exports = e => {
if (e instanceof ProcessingError1) {
// handle error thrown from process1
}
else if (e instanceof ProcessingError2) {
// handle error thrown from process2
}
else {
// handle other errors as needed..
}
}
我正在使用 axios 响应拦截器,在取消请求后,我需要打破承诺链。我不想为我的应用程序中的所有请求添加已取消请求的错误检查。我试过蓝鸟,但它似乎只是取消承诺,而不是断链。 我必须在第一次捕获时处理错误。这张图大体上显示了这个问题。 Latest then 和 catch 在不同的文件中。
Promise
.then((response) => {
)
.catch((error) => {
// break promise here
})
.then((response) => {
// skip
// I don't want any extra checks here!
)
.catch((error) => {
// skip
// I don't want any extra checks here!
})
最后只保留一个 catch 块来收集由您的承诺链触发的所有错误。
如果一个 promise 抛出链中的以下内容未被执行
Promise
.then((response) => {
)
.then((response) => {
// skip
// I don't want any extra checks here!
)
.catch((error) => {
// skip
// I don't want any extra checks here!
})
另一种选择是在最后抛出一个自定义错误,该错误可以在单个 catch
块中捕获,如下所示:
const errorHandler = require('path/to/handler')
class ProcessingError1 extends Error {
constructor(message) {
super(message);
this.name = "ProcessingError1";
}
}
class ProcessingError2 extends Error {
constructor(message) {
this.message = message;
this.name = "ProcessingError2";
}
}
const process1 = async () => {
throw new ProcessingError1("Somethign went wrong");
};
const process2 = async () => {
return { some: "process" };
};
const someApiCall = async () => ({ some: "response" });
someApiCall()
.then(process1)
.then(process2) // process2 is not run if process1 throws an error
.catch(errorHandler);
// ErrorHandler.js
module.exports = e => {
if (e instanceof ProcessingError1) {
// handle error thrown from process1
}
else if (e instanceof ProcessingError2) {
// handle error thrown from process2
}
else {
// handle other errors as needed..
}
}