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

后端 未结 6 1025
别那么骄傲
别那么骄傲 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:26

    There are some good answers here already. But it's worthwhile to drive home the difference in parallelism offered:

    • success() returns the original promise
    • then() returns a new promise

    The difference is then() drives sequential operations, since each call returns a new promise.

    $http.get(/*...*/).
      then(function seqFunc1(response){/*...*/}).
      then(function seqFunc2(response){/*...*/})
    
    1. $http.get()
    2. seqFunc1()
    3. seqFunc2()

    success() drives parallel operations, since handlers are chained on the same promise.

    $http(/*...*/).
      success(function parFunc1(data){/*...*/}).
      success(function parFunc2(data){/*...*/})
    
    1. $http.get()
    2. parFunc1(), parFunc2() in parallel

提交回复
热议问题