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

前端 未结 3 2338
星月不相逢
星月不相逢 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:49

    I think you're slightly misunderstanding how promises work.

    In your first code block there is only one promise object and it's SomeAsyncService.getData(). The errorCallback is not called here because that promise is resolved.

    In the second code block there are actually 2 promise objects you're working with. Note that .then() "returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback". So what's happening is you're catching the error from the second promise returned from SomeAsyncService.getData().then(...).

提交回复
热议问题