本文介绍了jQuery的延期和承诺-.then()vs .done()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关jQuery的递归和诺言,但看不到使用.then()和&的区别. .done()成功回调.我知道 Eric Hynds 提到了.done().success()映射具有相同的功能,但我猜.then()也是一样,因为所有回调都在成功完成操作后被调用.

I've been reading about jQuery deferreds and promises and I can't see the difference between using .then() & .done() for successful callbacks. I know Eric Hynds mentions that .done() and .success() map to the same functionality but I'm guessing so does .then() as all the callbacks are all invoked on a completion of a successful operation.

有人可以启发我正确使用吗?

Can anyone please enlighten me to the correct usage?

推荐答案

解决延迟问题后,将触发附加到done()的回调.拒绝延期时,将触发附加到fail()的回调.

The callbacks attached to done() will be fired when the deferred is resolved. The callbacks attached to fail() will be fired when the deferred is rejected.

在jQuery 1.8之前,then()只是语法糖:

Prior to jQuery 1.8, then() was just syntactic sugar:

promise.then( doneCallback, failCallback )
// was equivalent to
promise.done( doneCallback ).fail( failCallback )

从1.8开始,then()pipe()的别名,并返回新的承诺,请参见进行递延,以获取有关pipe()的更多信息.

As of 1.8, then() is an alias for pipe() and returns a new promise, see here for more information on pipe().

success()error()仅在通过调用ajax()返回的jqXHR对象上可用.它们分别是done()fail()的简单别名:

success() and error() are only available on the jqXHR object returned by a call to ajax(). They are simple aliases for done() and fail() respectively:

jqXHR.done === jqXHR.success
jqXHR.fail === jqXHR.error

此外,done()不仅限于单个回调,而且会过滤掉非功能性(尽管1.8版中存在字符串错误,应在1.8.1中修复):

Also, done() is not limited to a single callback and will filter out non-functions (though there is a bug with strings in version 1.8 that should be fixed in 1.8.1):

// this will add fn1 to 7 to the deferred's internal callback list
// (true, 56 and "omg" will be ignored)
promise.done( fn1, fn2, true, [ fn3, [ fn4, 56, fn5 ], "omg", fn6 ], fn7 );

fail()也是如此.

这篇关于jQuery的延期和承诺-.then()vs .done()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 21:05