How to stop/break in the middle of chained promises

前端 未结 2 1606
遇见更好的自我
遇见更好的自我 2021-01-13 19:27

I have a chain of $http calls to server. If one call fails I want to display notification to user and stop the chain. At first I thought I can use the $q.reject to stop the

2条回答
  •  暖寄归人
    2021-01-13 19:47

    From the $q.reject documentation:

    When comparing deferreds/promises to the familiar behavior of try/catch/throw, think of reject as the throw keyword in JavaScript. This also means that if you "catch" an error via a promise error callback and you want to forward the error to the promise derived from the current promise, you have to "rethrow" the error by returning a rejection constructed via reject.

    reject won't automatically abort the promise chain, it will just continue to the next promise, calling the error handler for each promise remaining.

    Also, the finally callback is always going to run regardless of whether the chain is errored or not. If you don't want it to run then it's up to you to check the status of the promise manually.

    EDIT:

    Here's a link to an answer that shows how to chain errors:

    Break promise chain and call a function based on the step in the chain where it is broken (rejected)

提交回复
热议问题