0

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)

2

2 Answers 2

1

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

Because you are not calling functionA due to your condition1 being false.

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

Wrap it in a try catch block. Modified code to tailor what you are asking for.

Here's the assert from this great article: 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)

Sign up to request clarification or add additional context in comments.

Comments

0

Why are you return Promise in async function. When you write async JS will wrapped your function in Promise.

Well all your function returns Promise it means that you have to use 2 times async

1 Comment

I guess async is just a wrapper... I need to return a Promise resolve/reject

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.