dimanche 23 avril 2017

Stop Promise Chain execution in a recommended way

I have a code similar to this one :

promise_function().then(()=>{
  //do something
  return another_promise_fucntion();
}).then(() => {
  //do something
  return another_promise_function1();
}).then((result) => {
  //check if result is valid
  if(!result)
     //break chain (how to stop calling the next .then functions ?)
  else
     return another_promise_function2();
}).then(()=>{
   //do something
   return another_promise_function3();
}).catch((err)=>{
   //handle error
});

I want to stop calling the next .then() functions, if the returned result is not valid.

I used "throw new Error()", and it worked just fine, but I am not sure if it is the recommended way.




Aucun commentaire:

Enregistrer un commentaire