首页 文章

Angular HttpPromise:`success` /`error`方法和`then`参数之间的区别

提问于
浏览
173

根据AngularJS doc,对 $http 的调用返回以下内容:

使用标准then方法和两个http特定方法返回promise对象:成功和错误 . then方法使用两个参数成功,并使用响应对象调用错误回调 . 成功和错误方法采用单个参数 - 在请求成功或失败时将分别调用的函数 . 传递给这些函数的参数是传递给then方法的响应对象的析构表示 .

除了在一种情况下 response 对象被解构的事实之外,我没有区别

  • 传递的成功/错误回调作为 promise.then 的参数传递

  • 作为promise的 promise.success / promise.error 方法的参数传递的回调

有没有?这两种不同方式通过看似相同的回调有什么意义?

6 回答

  • 17

    NB这个答案事实上是不正确的;正如下面的评论所指出的, success() does return the original promise. 我不会改变;并将其留给OP进行编辑 .


    2之间的主要区别在于 .then() 调用返回一个promise(使用从回调返回的值解析),而 .success() 是更传统的注册回调方式并且不返回promise .

    基于承诺的回调( .then() )可以轻松链接承诺(进行调用,解释结果然后再进行一次调用,解释结果,再做一次调用等) .

    当您不需要链接调用或使用promise API时(例如,在路由中), .success() 方法是一种简化的方便方法 .

    简而言之:

    • .then() - promise API的全部功能,但稍微冗长一些

    • .success() - 不会返回一个承诺,但会提供稍微方便的语法

  • 2

    这里有一些很好的答案 . 但是,将并行性的差异推向家庭是值得的:

    • success() 返回原始承诺

    • then() 返回一个新的承诺

    不同之处在于 then() 驱动顺序操作,因为每次调用都会返回一个新的promise .

    $http.get(/*...*/).
      then(function seqFunc1(response){/*...*/}).
      then(function seqFunc2(response){/*...*/})
    
    • $http.get()

    • seqFunc1()

    • seqFunc2()

    success() 驱动并行操作,因为处理程序链接在同一个promise上 .

    $http(/*...*/).
      success(function parFunc1(data){/*...*/}).
      success(function parFunc2(data){/*...*/})
    
    • $http.get()

    • parFunc1()parFunc2() 并行

  • 153

    简单GET请求的一些代码示例 . 也许这有助于理解差异 . 使用 then

    $http.get('/someURL').then(function(response) {
        var data = response.data,
            status = response.status,
            header = response.header,
            config = response.config;
        // success handler
    }, function(response) {
        var data = response.data,
            status = response.status,
            header = response.header,
            config = response.config;
        // error handler
    });
    

    使用 success / error

    $http.get('/someURL').success(function(data, status, header, config) {
        // success handler
    }).error(function(data, status, header, config) {
        // error handler
    });
    
  • 199

    .then()是可链接的,将等待之前的.then()来解决 .

    .success()和.error()可以链接,但它们都会立刻触发(所以没那么多)

    .success()和.error()非常适合简单的调用(简单的制作者):

    $http.post('/getUser').success(function(user){ 
       ... 
    })
    

    所以你不必输入这个:

    $http.post('getUser').then(function(response){
      var user = response.data;
    })
    

    但通常我用.catch()处理所有错误:

    $http.get(...)
        .then(function(response){ 
          // successHandler
          // do some stuff
          return $http.get('/somethingelse') // get more data
        })
        .then(anotherSuccessHandler)
        .catch(errorHandler)
    

    如果你需要支持<= IE8,那么就像这样编写.catch()和.finally()(IE中的保留方法):

    .then(successHandler)
        ['catch'](errorHandler)
    

    Working Examples:

    这是我用更多的编码格式编写的内容,用于刷新我的内存,了解它如何处理错误等:

    http://jsfiddle.net/nalberg/v95tekz2/

  • 110

    只是为了完成,这是一个代表示例差异的代码示例:

    成功\错误:

    $http.get('/someURL')
    .success(function(data, status, header, config) {
        // success handler
    })
    .error(function(data, status, header, config) {
        // error handler
    });
    

    然后:

    $http.get('/someURL')
    .then(function(response) {
        // success handler
    }, function(response) {
        // error handler
    })
    .then(function(response) {
        // success handler
    }, function(response) {
        // error handler
    })
    .then(function(response) {
        // success handler
    }, function(response) {
        // error handler
    }).
    
  • 27

    Official Notice: success and error have been deprecated, please use the standard then method instead.

    弃用通知:已弃用$ http遗留承诺方法成功与错误 . 请改用标准方法 . 如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误 .

    link: https://code.angularjs.org/1.5.7/docs/api/ng/service/$http

    screenshot: view the screenshot

相关问题