1

Is there an easier/proper way for when a promise error's for it to bubble up to when it is called in a second promise without having to reject the promise in it's catch block each time manually?

promise1() {
return this.$http
  .get("/url")
  .then(response => {
    // do things
  })
  .catch(err => {
    return Promise.reject(err); 
    // don't want to have to call this in every single catch block
    // ideally errors would bubble up when called from second promise
  });

}

In a second promise, I want the catch block to be triggered when the first promise fails (and not having to manually reject it)

promise2(){
    return .promise1()
           .then(response => {})
           .catch(err => {
                // error bubbled up from first promise
            }
          });
}
  • 1
    yes, simply get rid of the `.catch()` in `promise1()`. Plus, it's completely pointless to catch an error, just to immediately throw the very same error, without even logging or doing anything else. – Thomas Sep 22 '17 at 18:05
  • @jfriend00, take another look. This ain't the same question. He's not handling the error, he's re-throwing. – Thomas Sep 22 '17 at 19:08
  • 1
    @Thomas - He's asking why he has to rethrow or how to not rethrow which is addressed in many, many other answers here. – jfriend00 Sep 22 '17 at 19:09
  • If you have no `.catch()` it will automatically bubble up the chain until the next `.catch()`. If you insert a `.catch()`, you MUST rethrow or return rejected promise to continue the chain as rejected. This works analagously to `try/catch`. The first `catch` to see the exception/rejection stops it unless rethrown. – jfriend00 Sep 22 '17 at 19:10

0 Answers0