假设我async设置了三个函数,如下所示:
const stepOne = async () => { setTimeout(function() {
console.log("step 1")
}, 3000) }
const stepTwo = async () => { throw new Error("Error at step two") }
const stepThree = async () => { console.log("step 3") }
我将如何按顺序执行所有这些函数并在 stepTwo 处打破承诺链,不允许 stepThree 函数运行?
所以,
正常顺序是这样的:stepOne --> stepTwo --> stepThree
在 stepTwo 处抛出错误的序列:stepOne --> stepTwo
在 stepTwo 抛出的错误需要在 end catch 块中被捕获。
更新#1:错过了问题的关键要素。await 不能使用,因为这三个函数需要在非异步函数中调用。
例子:
const testFunc = () => {
resolve three promises
sequentially, break the promise chain when error is thrown
and ultimately, catch errors here
}
12345678_0001
慕丝7291255
素胚勾勒不出你
相关分类