In Angular, what's the conceptual difference between the error and catch functions for promises?

前端 未结 3 2249
星月不相逢
星月不相逢 2021-02-19 03:16

I finally got Angular promise error handling down but it was counter-intuitive to me. I expected errors to be handled by the failure callback but instead I had to use a catch. <

3条回答
  •  -上瘾入骨i
    2021-02-19 03:28

    By angularJS documentation for $q:

    Methods

    then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available.

    .....

    catch(errorCallback) – shorthand for promise.then(null, errorCallback)

    The two pieces of code you posted are identical.

提交回复
热议问题