延迟与 $.ajax done/fail

Deferreds vs $.ajax done/fail

对于独立的异步操作,我究竟应该使用什么?延迟对象还是 ajax done()/fail() 就足够了?有什么区别?

我知道 deferred.when() 很有用,因为它让我们知道所有 多个 异步操作何时完成。但是 一个 异步操作呢? done()/fail() 让我也知道什么时候完成。

$.ajax() 方法 return 是一个 $.Deferred() 对象。所以你在所有情况下都使用延迟。您可以自己设置延迟,但 ajax 会自动 return 相同。这仅适用于 jQuery,原版 javascript 没有此功能。

$.ajax() 将 return 以下承诺方法:

fail()done()always()then().


Jquery 论坛说了什么:

The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise

jQuery $.ajax 的详细参考可在此处获得,http://api.jquery.com/jQuery.ajax/

来自Documentation

jQuery.Deferred() introduces several enhancements to the way callbacks are managed and invoked. In particular, jQuery.Deferred() provides flexible ways to provide multiple callbacks, and these callbacks can be invoked regardless of whether the original callback dispatch has already occurred.

并且:

One model for understanding Deferred is to think of it as a chain-aware function wrapper.

所以就像你说的,最大的区别是 deferred.when 在处理多个请求时很有用。回答你的问题,对于独立操作,done()fail() 会做得很好,并且更容易实现,在我看来 c: