使用 promise 纠正嵌套异步函数的错误处理
Correct error handling of nested async functions with promise
让我们以异步函数 asyncFunction
为例,它采用布尔参数 res
。如果 res
为真,promise 将解析为字符串“success”。如果 res
为 false,promise 将拒绝并返回一个包含字符串“success”的错误对象。
const asyncFunction = (res) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (res) return resolve("Success");
return reject(new Error("Error"));
}, 1000);
});
};
我在使用 try-catch 块时从函数 innerFunction
调用 asyncFunction
。到目前为止一切顺利。
const innerFunction = async () => {
try {
return await asyncFunction(false); //Will reject
} catch (error) {
console.log("caught error inside innerFunction");
return error;
}
};
在outerFunction
中,我正在执行
const outerFunction = async () => {
try {
const result = await innerFunction();
console.log(result);
} catch (error) {
console.log("caught error inside outerFunction");
console.log(error);
}
};
最后,我是运行外部函数。
outerFunction();
我原以为两个 catch 块都能捕捉到错误,但我错了。
我猜你不能两次捕获相同的异常?我不确定,但我的猜测是 Error 对象在幕后被标记为“已处理”错误,因此不会被识别为 outerFunction
中的一个。 (我希望对此有所了解!)
我的解决方案是在 innerFunction 中抛出一个新的错误:
const innerFunction = async () => {
try {
return await asyncFunction(false); //Will reject
} catch (error) {
console.log("caught error inside innerFunction");
throw new Error(error);
}
};
对于篇幅,我深表歉意,但最后,我的问题是:这是处理我必须处理 outerFunction
中错误的情况的“正确”方式吗?有没有“更好”的方法来做到这一点?
谢谢!
不,你不能两次捕获相同的错误。中继错误的方法将是错误的。相反,我要做的是转发输出并在外部函数中处理它们。
const innerFunction = async () => asyncFunction(false); // <-- Just return the outputs
const outerFunction = async () => {
try {
const result = await innerFunction(); // <-- Error is forwarded and thus handled
} catch (error) {
console.log("Caught error inside outerFunction");
}
};
让我们以异步函数 asyncFunction
为例,它采用布尔参数 res
。如果 res
为真,promise 将解析为字符串“success”。如果 res
为 false,promise 将拒绝并返回一个包含字符串“success”的错误对象。
const asyncFunction = (res) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (res) return resolve("Success");
return reject(new Error("Error"));
}, 1000);
});
};
我在使用 try-catch 块时从函数 innerFunction
调用 asyncFunction
。到目前为止一切顺利。
const innerFunction = async () => {
try {
return await asyncFunction(false); //Will reject
} catch (error) {
console.log("caught error inside innerFunction");
return error;
}
};
在outerFunction
中,我正在执行
const outerFunction = async () => {
try {
const result = await innerFunction();
console.log(result);
} catch (error) {
console.log("caught error inside outerFunction");
console.log(error);
}
};
最后,我是运行外部函数。
outerFunction();
我原以为两个 catch 块都能捕捉到错误,但我错了。
我猜你不能两次捕获相同的异常?我不确定,但我的猜测是 Error 对象在幕后被标记为“已处理”错误,因此不会被识别为 outerFunction
中的一个。 (我希望对此有所了解!)
我的解决方案是在 innerFunction 中抛出一个新的错误:
const innerFunction = async () => {
try {
return await asyncFunction(false); //Will reject
} catch (error) {
console.log("caught error inside innerFunction");
throw new Error(error);
}
};
对于篇幅,我深表歉意,但最后,我的问题是:这是处理我必须处理 outerFunction
中错误的情况的“正确”方式吗?有没有“更好”的方法来做到这一点?
谢谢!
不,你不能两次捕获相同的错误。中继错误的方法将是错误的。相反,我要做的是转发输出并在外部函数中处理它们。
const innerFunction = async () => asyncFunction(false); // <-- Just return the outputs
const outerFunction = async () => {
try {
const result = await innerFunction(); // <-- Error is forwarded and thus handled
} catch (error) {
console.log("Caught error inside outerFunction");
}
};