我正在测试任务中三个异步/等待功能(功能A,功能B,功能C)的条件序列 但是,当我执行它时,指出第一个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)
答案 0 :(得分:1)
1-我没有获得预期的功能失败消息
因为您的functionA
是condition1
,所以您没有呼叫false
。
2-当有任何功能时,如何处理错误以停止序列 没有通过?
将其包装在try catch块中。修改后的代码可定制您的要求。
这是这篇很棒的文章的断言:https://blog.patricktriest.com/what-is-async-await-why-should-you-care/
在这里,我们将整个操作包装在正常的try / catch中 块。这样,我们可以抛出并捕获同步代码中的错误 与异步代码完全相同。简单得多。
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)
答案 1 :(得分:0)
为什么要在异步函数中返回Promise。当您编写异步JS时,JS会将您的函数包装在Promise中。
所有函数都返回Promise,这意味着您必须使用2次异步