How angular promise .then works

后端 未结 2 914
清歌不尽
清歌不尽 2021-01-15 09:56

I am new to AngularJS $q and promise, I learn that if I use $http.get(), I can get a promise and I can use its .then function chainly like

2条回答
  •  醉话见心
    2021-01-15 10:12

    how do I know that then has been resolved?

    As you noticed, the promise returned by then() is resolved after the callback is executed (which is executed when the first promise is fulfilled). And it does resolve with the result from the callback indeed.

    Do I need give a return as resolve?

    No, if you don't return anything from the callback, the result is undefined - just like with regular function calls.

    However, you do in fact have more options to build a result in your callback than returning a plain value:

    • You can return another promise. The promise returned by then() will adopt it, i.e. fulfills with its result or reject with its reason as soon as it settles.
    • You can throw an exception. It will get automatically caught and the resulting promise is rejected with that error.

提交回复
热议问题