8

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.

Abderrahman Hilali
  • 211
  • 1
  • 3
  • 13

1 Answers1

1

It's correct way which recommenced by Mozilla You can see more detail here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

taile
  • 2,738
  • 17
  • 29