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

时间:2022-02-18 18:50:57

Seems to me that both does the same thing.

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

Docs:

文档:

4 个解决方案

#1


31  

It would seem that deferred.then() allows you to pass two separate callbacks for success and failure, whereas deferred.always() takes n number 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()

#2


7  

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

使用.then(),您可以在$ .Deferred解析(完成)时提供单独的回调,并在$ .Deferred被拒绝(失败)时提供另一个回调。

.always(), on the other hand, allows you to provide a callback that always gets executed, whether the $.Deferred has 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 $.Deferred was 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 always want to hide the progress bar, no matter if the $.Deferred was resolved or rejected:

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

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

#3


6  

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/)

#4


0  

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)

当时(从1.8开始)的最大好处是能够明确地链接任务,因为它返回一个将通过回调的结果解决的promise。

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
});

#1


31  

It would seem that deferred.then() allows you to pass two separate callbacks for success and failure, whereas deferred.always() takes n number 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()

#2


7  

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

使用.then(),您可以在$ .Deferred解析(完成)时提供单独的回调,并在$ .Deferred被拒绝(失败)时提供另一个回调。

.always(), on the other hand, allows you to provide a callback that always gets executed, whether the $.Deferred has 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 $.Deferred was 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 always want to hide the progress bar, no matter if the $.Deferred was resolved or rejected:

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

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

#3


6  

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/)

#4


0  

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)

当时(从1.8开始)的最大好处是能够明确地链接任务,因为它返回一个将通过回调的结果解决的promise。

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
});