Async/await (javascript ch.- 2)

how can i use catch with async function ?

async function withAsync(num) {
  if (num === 0){
      return 'zero';
    } else {
      return 'not zero';
    }
}

// Leave this commented out until step 3:

withAsync(10)
  .then((resolveValue) => {
  console.log(` withAsync(100) returned a promise which resolved to: ${resolveValue}.`);
})
.catch((rejectValue) => {
  console.log(`rejected value is ${rejectValue}`);
})

this print log of then only.

Your promise resolves all the time. ‘Else’ is not detected as an error. If you throw an error, it is:

async function withAsync(num) {
  if (num === 0){
      return 'zero';
    } else {
      throw 'not zero';
    }
}
1 Like

Perfect, Thanks! :+1: :+1:

1 Like