0

I want to do a promise chain like this one:

promise1
  .then(resp => {
    ... // some code to run after promise1 
    if (xxx) {
      return promise2() // call promise2
    } else {
      // I want to stop the promise chain here
    }
  })
  .then(resp => {
    ... // some code to run after promise2 
  })
  .catch(error => {
    ... // I only want to handle error in catch function
  })

What is the best way to create optional promise chains?

What is the best way to stop promise chains?

KevinHu
  • 991
  • 3
  • 10
  • 20
  • You should chain the `then` that you want to run only after `promise2()` only to that promise. – Bergi May 10 '17 at 03:01
  • but isn't that generate a callback hell !? If I have more login to handle..Is that the best way ..? – KevinHu May 10 '17 at 03:04
  • No, it's just nesting conditional blocks as usual. Don't forget that [with promises you always `return`](http://stackoverflow.com/a/22562045/1048572), instead of heading towards a callback (that can be missed). – Bergi May 10 '17 at 03:35
  • so.. if I have some conditionals in promise2 to call promise3 or not, I will be just nesting ..? – KevinHu May 10 '17 at 03:43
  • Yes. Just like you'd be nesting `if` blocks. – Bergi May 10 '17 at 03:44

0 Answers0