Angular HttpPromise: difference between `success`/`error` methods and `then`'s arguments

后端 未结 6 999
别那么骄傲
别那么骄傲 2020-11-22 16:59

According to AngularJS doc, calls to $http return the following:

Returns a promise object with the standard then

6条回答
  •  无人及你
    2020-11-22 17:47

    NB This answer is factually incorrect; as pointed out by a comment below, success() does return the original promise. I'll not change; and leave it to OP to edit.


    The major difference between the 2 is that .then() call returns a promise (resolved with a value returned from a callback) while .success() is more traditional way of registering callbacks and doesn't return a promise.

    Promise-based callbacks (.then()) make it easy to chain promises (do a call, interpret results and then do another call, interpret results, do yet another call etc.).

    The .success() method is a streamlined, convenience method when you don't need to chain call nor work with the promise API (for example, in routing).

    In short:

    • .then() - full power of the promise API but slightly more verbose
    • .success() - doesn't return a promise but offeres slightly more convienient syntax

提交回复
热议问题