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

前端 未结 3 2245
星月不相逢
星月不相逢 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条回答
  •  隐瞒了意图╮
    2021-02-19 03:24

    The second argument should be almost never be used literally in application code while also using the first. It is mostly about promise library interoperability between different implementations.

    You should always use .catch unless you specifically have some weird corner case where you need .then(succcess, fail).

    See The .then(success, fail) anti-pattern.

    Also Q library (The one angular $q is based on) has similar section in their readme

提交回复
热议问题