1

I'm a beginner in JS and I have a question about Promises.

Context: I have a basic promise with 3 callbacks:

const calcul = (num1, num2) =>{
  return new Promise((resolve,reject) => {
    const result = num1 * num2;
    if(result > 20000){
      resolve(result);
    }else{
      reject();
    }
  })
}

const fun1 = (result) => {
  console.log('Result: ' + result);
};

const fun2 = () => {
  console.log('Oups, error');
};

const fun3 = () => {
  console.log('Oups, another error');
};

What is the difference between:

calcul(300,9).then(fun1).catch(fun2);

and

calcul(300,9).then(fun1,fun2);

What is the difference between:

calcul(300,90).then(fun1).catch(fun2).then(() => {console.log("FINALLY")});

and

calcul(300,900).then(fun1).catch(fun2).finally(() => {console.log("FINALLY")});

Thanks

Bergi
  • 630,263
  • 148
  • 957
  • 1,375

1 Answers1

1

So in the first case, the difference is that in the catch, you are handling the promise returned by then (which may just be an unhandled rejected promise from calcul, or a problem in the then handler) while in the then(func,func) one, you are handling either a successful promise returned by calcul or a rejected promise returned by calcul. Make sense?

In the second case a difference is that then would not be called if catch throws an error, while finally will be called always.

chad_
  • 3,749
  • 2
  • 22
  • 22