I am testing a conditional sequence of three async/await functions ( functionA, functionB, functionC) in a task however when I execute it, stating that the first functionA should not pass,
1 - I don't get the expected functionA failed message
2 - how should I handle errors to stop the sequence when any function is not passing ?
thanks for feedback
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)
new Promiseto a minimum and use async for the rest, e.g. gist.github.com/loganfsmyth/b8731fca09b88ad52333c4aa90f0081e