猿问

如果序列中的承诺之一抛出错误,则按顺序解决承诺并打破序列

假设我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

   

  }


翻过高山走不出你
浏览 154回答 3
3回答

12345678_0001

如果您解决承诺,您的代码将起作用stepOne,因为setTimeout只是将函数添加到堆栈而不是等待它解决。如果您要返回一个 PromisestepOne并在之后解决它,console.log那么它将try catch等待stepOne并捕获错误stepTwo这是您的代码示例const stepOne = async () => {    return new Promise((resolve, reject) => {        setTimeout(function() {            console.log("step 1")            resolve(true);        }, 3000)    });}const stepTwo = async () => { throw new Error("Error at step two") }const stepThree = async () => {    return new Promise((resolve, reject) => {        setTimeout(function() {            console.log("step 3")            resolve(true);        }, 3000)    });}(() => {    stepOne()        .then(stepTwo)        .then(stepThree)        .catch(error => {            console.log(error);        })  })();现在console.log看起来像这样step 1Error: Error at step two    at stepTwo (/home/user/develop/test/stackoverflow.js:10:38)    at processTicksAndRejections (internal/process/task_queues.js:93:5)

慕丝7291255

请尝试以下代码。您需要等待每次调用(stepOne、stepTwo 和 stepThree),以便在出现异常时不会进行下一次调用。try {    await stepOne();    await stepTwo();    await stepThree()} catch (error) {    console.log(error);}

素胚勾勒不出你

如果您的步骤是返回 Promise 的函数,您可以创建一个包装函数,它将按顺序调用每个步骤并在步骤失败时中止,并记录失败步骤的详细信息。在此示例中,每个步骤失败的概率为五分之一。// Make a step proc, that throws 1 time in 5function createStep(index) {&nbsp; &nbsp; let error = (Math.random() < 0.2) ? new Error(`Step ${index+1} error`) : null ;&nbsp; &nbsp; return () => new Promise((resolve, reject) => setTimeout(error ? reject(error): resolve(`Step ${index+1} outcome`), 500));}async function runSteps(steps) {&nbsp; &nbsp;&nbsp; &nbsp;for(stepIndex = 0; stepIndex < steps.length; stepIndex++) {&nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(`Running step #${stepIndex+1}...`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let result = await steps[stepIndex]();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(`Step result:`, result);&nbsp; &nbsp; &nbsp; &nbsp;} catch (e) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.error(`An error occurred at step #${stepIndex+1}:`, e.message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if (stepIndex === (steps.length -1) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("All steps completed successfully");&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}let steps = Array.from( { length: 3 }, (v,k) => createStep(k));runSteps(steps);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答