Deferred object文档说它可以采用可选的函数参数,但没有一个例子。我该如何使用?
答案 0 :(得分:1)
这是一个语法糖,通过它可以在同一个延迟对象上添加一个回调/处理程序来解析或拒绝。
所以,以下两个代码片段将做同样的事情。
// with constructor method argument
var x = $.Deferred(function(selfDfd){
selfDfd.done( console.log.bind(console,'I am resolved,in constructor'));
});
x.resolve();
// without constructor method argument
var y = $.Deferred();
y.done(console.log.bind(console,'I am resolved, NO constructor args'))
y.resolve();
答案 1 :(得分:0)
使用以下语法:
function foo(){ console.log("Yes!!!")}
function bar(){ console.log("Are we there yet?")}
function baz(){ console.log("I can't hold it much longer!?")}
with($.Deferred() )
{
done(foo);
bar();
baz();
resolve(foo);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
发电机会产生同样的效果:
var foobarbaz = function(i){var count = count || i; return function(){console.log("Yes!!!"); return ++count;}}
var bar = function(){ console.log("Are we there yet?")}
var baz = function(){ console.log("I can't hold it much longer!?")}
var done = foobarbaz(0);
bar();
baz();
done();
<强>参考强>