javascript jQuery:deferred.always() 和 deferred.then() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12448171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:13:04  来源:igfitidea点击:

jQuery: What is the difference between deferred.always() and deferred.then()?

javascriptjqueryjquery-deferred

提问by Niyaz

Seems to me that both does the same thing.

在我看来,两者都做同样的事情。

Docs:

文档:

回答by JamesHalsall

It would seem that deferred.then()allows you to pass two separate callbacks for success and failure, whereas deferred.always()takes nnumber of callbacks which will all be called regardless of the outcome of the initial event.

这似乎是deferred.then()可以让你通过对成功和失败两个单独的回调,而deferred.always()需要n回调将全部不管初始事件的结果叫号。

I would say use deferred.always()in the cases where success/failure of the initial event are not important

我会说deferred.always()在初始事件的成功/失败并不重要的情况下使用

回答by Jo?o Silva

With .then()you can provide an individual callback for when the $.Deferredis resolved (done), and anotherfor when the $.Deferredis rejected (fail).

随着.then()您可以提供当为单个回调$.Deferred解决了(done),和另一个当为$.Deferred被拒绝(fail)。

.always(), on the other hand, allows you to provide a callback that always gets executed, whether the $.Deferredhas been resolved or rejected. In other words, within this callback, it doesn't matter if the AJAX call has failed or has been been successfully executed.

.always(),另一方面,允许您提供始终执行的回调,无论$.Deferred已解决还是被拒绝。换句话说,在这个回调中,AJAX 调用是失败还是成功执行都没有关系。

I tend to put code in .always()when I want that code to run everytime, and independently of whether the $.Deferredwas resolved successfully or not. For example, to clear an AJAX loading indicator or to hide a progress bar. Using .then()you'd have something like this:

.always()当我希望代码每次都运行时,我倾向于将代码放入,并且与是否$.Deferred成功解决无关。例如,清除 AJAX 加载指示器或隐藏进度条。使用.then()你会有这样的事情:

$.get("/some/url").then(function () { // done callback
  $(".progress-bar").hide();
}, function () { // fail callback
  $(".progress-bar").hide();
});

Whilst if you used .always(), you'd just need a single callback, because you alwayswant to hide the progress bar, no matter if the $.Deferredwas resolved or rejected:

而如果你使用了.always(),你只需要一个回调,因为你总是想隐藏进度条,无论是$.Deferred被解决还是被拒绝:

$.get("/some/url").always(function () {
  $(".progress-bar").hide();
});

回答by Ignitor

Prior to jQuery 1.8: .always(fn)is equivalent to .then(fn, fn)

jQuery 1.8 之前:.always(fn)相当于.then(fn, fn)

As of jQuery 1.8: .always(fn)is similar to .then(fn, fn)but it differs in what is returned (see http://api.jquery.com/deferred.then/for details)

从 jQuery 1.8 开始:.always(fn)类似于.then(fn, fn)但返回的内容有所不同(有关详细信息,请参阅http://api.jquery.com/deferred.then/

回答by schlamar

The big benefit of then(as of 1.8) is the capability to chain tasks explicitly because it returns a promise which will be resolved with the result of the callback(s)

then(从 1.8 开始)的最大好处是能够显式链接任务,因为它返回一个承诺,该承诺将通过回调的结果解决

Example from documentation:

文档中的示例:

var request = $.ajax( url, { dataType: "json" } ),
    chained = request.then(function( data ) {
      return $.ajax( url2, { data: { user: data.userId } } );
    });

chained.done(function( data ) {
  // data retrieved from url2 as provided by the first request
});