Terminate the promise in the recommended order

I have code similar to this:

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 following .then () functions if the return result is invalid.

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

+4
source share
1 answer

This is the correct way that Mozilla resumed, you can see in more detail here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

+1
source

Source: https://habr.com/ru/post/1675441/


All Articles