gpt4 book ai didi

javascript - ES6 javascript 如何处理一系列 async/await 函数中的错误

转载 作者:行者123 更新时间:2023-12-03 01:29:02 25 4
gpt4 key购买 nike

我正在任务中测试三个异步/等待函数(functionA、functionB、functionC)的条件序列但是当我执行它时,指出第一个 functionA 不应该通过,

1 - 我没有收到预期的功能失败消息

2 - 当任何函数未通过时,我应该如何处理错误以停止序列?

感谢反馈

console.log

 functionB: functionB passed
functionC: functionC passed
task ended

ES6 js

async function functionA(duration, shouldPass) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldPass) {
resolve('functionA passed');
} else {
reject(Error('functionA failed'));
}
}, duration);
});
}

async function functionB(duration, shouldPass) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldPass) {
resolve('functionB passed');
} else {
reject(Error('functionB failed'));
}
}, duration);
});
}

async function functionC(duration, shouldPass) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldPass) {
resolve('functionC passed');
} else {
reject(Error('functionC failed'));
}
}, duration);
});
}

async function executeAsyncTask(condition1, condition2, condition3) {
let resultFunctionA = true
if (condition1) {
resultFunctionA = await functionA(3000, true)
console.log('functionA: ', resultFunctionA)
}
let resultFunctionB = true
if (resultFunctionA && condition2) {
resultFunctionB = await functionB(3000, true)
console.log('functionB: ', resultFunctionB)
}
let resultFunctionC = true
if (resultFunctionB && condition3) {
resultFunctionC = await functionC(3000, true)
console.log('functionC: ', resultFunctionC)
}
console.log('task ended')
}

// running task with condition1, condition2, condition3 parameters
executeAsyncTask(false, true, true)

最佳答案

1 - I don't get the expected functionA failed message

因为您没有调用 functionA,因为您的 condition1false

2 - how should I handle errors to stop the sequence when any function is not passing ?

将其包装在 try catch block 中。修改代码以适应您的要求。

这是这篇精彩文章的断言:https://blog.patricktriest.com/what-is-async-await-why-should-you-care/

Here, we've wrapped the entire operation within a normal try/catch block. This way, we can throw and catch errors from synchronous code and asynchronous code in the exact same way. Much simpler.

async function functionA(duration, shouldPass) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldPass) {
resolve('functionA passed');
} else {
reject(Error('functionA failed'));
}
}, duration);
});
}

async function functionB(duration, shouldPass) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldPass) {
resolve('functionB passed');
} else {
reject(Error('functionB failed'));
}
}, duration);
});
}

async function functionC(duration, shouldPass) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldPass) {
resolve('functionC passed');
} else {
reject(Error('functionC failed'));
}
}, duration);
});
}

async function executeAsyncTask(condition1, condition2, condition3) {
try {
let resultFunctionA = await functionA(3000, condition1)
console.log('functionA: ', resultFunctionA)
let resultFunctionB = await functionB(3000, condition2)
console.log('functionB: ', resultFunctionB)
let resultFunctionC = await functionC(3000, condition3)
console.log('functionC: ', resultFunctionC)
console.log('task ended')
} catch (err) {
console.error(err.message)
}
}

// running task with condition1, condition2, condition3 parameters
executeAsyncTask(false, true, true)

关于javascript - ES6 javascript 如何处理一系列 async/await 函数中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51382549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com