KevinHu
KevinHu

Reputation: 1031

broken promise chaining in then callback function

How to stop the promise in chaining here:

    .then(resp => {
      if (xxxxx) {
        return nextPromise()
      } else {
        // stop promise chain
        return
      }
    })
    .then(resp => {
       // nextPromise callback function..
    })

I thought the return would stop the chain but I was wrong.

Upvotes: 0

Views: 63

Answers (1)

robertklep
robertklep

Reputation: 203419

If you don't want to break the chain by throwing an error, a solution would be to nest the chain:

.then(resp => {
  if (xxxxx) {
    return nextPromise().then(resp => {
      // nextPromise callback function..
    });
  }
})

This would still allow a global .catch() in which you don't have to explicitly check for an error thrown to end the chain. A drawback would be that if you have many of these conditions, you end up with something similar to "callback hell".

Upvotes: 1

Related Questions