jquery $.when() .then() 和 .done() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14259203/
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
jquery $.when() difference between .then() and .done()
提问by Salvador Dali
I am trying to understand the difference between
我试图理解两者之间的区别
$.when(...).then(...)
$.when(...).done(...)
in jQuery. As far as I understood both of them executes when objects inside when are finished loading. But what is the difference. Examples will be really appreciated.
在 jQuery 中。据我了解,当里面的对象加载完成时,它们都会执行。但有什么区别。示例将不胜感激。
回答by Gurpreet Singh
.done()
has only success callback.
.done()
只有成功回调。
.then()
has both success and fail callbacks.
.then()
有成功和失败回调。
As of jQuery 1.8, the
deferred.then()
method returns a new promise that can filter the status and values of a deferred through a function, replacing the now-deprecateddeferred.pipe()
method.The
deferred.done()
method accepts one or more arguments, all of which can be either a single function or an array of functions.Since
deferred.done()
returns the deferred object, other methods of the deferred object can be chained to this one, including additional.done()
methods. When the Deferred is resolved, doneCallbacks are executed using the arguments provided to the resolve or resolveWith method call in the order they were added.
从 jQuery 1.8 开始,该
deferred.then()
方法返回一个新的 Promise,可以通过函数过滤延迟的状态和值,替换现在已弃用的deferred.pipe()
方法。该
deferred.done()
方法接受一个或多个参数,所有参数都可以是单个函数或函数数组。由于
deferred.done()
返回延迟对象,延迟对象的其他方法可以链接到这个.done()
方法,包括附加 方法。当 Deferred 被解析时,doneCallbacks 将使用提供给 resolve 或 resolveWith 方法调用的参数按照它们添加的顺序执行。