【发布时间】:2013-11-21 13:25:00
【问题描述】:
如果我想让同步和异步函数按特定顺序执行,我可以使用 jQuery Promise,但它似乎不像我期望的那样工作。
在调用 deferred.resolve() 时,函数 a、b 和 c 应按此顺序执行我希望函数 b 会被执行,但无论是否调用 resolve,所有函数都会立即执行。
代码如下:
function a(){
var deferred = $.Deferred();
setTimeout(function(){
console.log("status in a:",deferred.state());
//this should trigger calling a or not?
deferred.resolve("from a");
},200);
console.log("a");
return deferred.promise();
};
function b(){
var deferred = $.Deferred();
setTimeout(function(){
console.log("status in b:",deferred.state());
deferred.resolve("from b");
},200);
console.log("b");
return deferred.promise();
}
//synchronous function
function c(){
var deferred = $.Deferred();
console.log("c");
console.log("status in c:",deferred.state());
deferred.resolve("from c");
return deferred.promise();
}
function test(){
fn=[a,b,c],i=-1,
len = fn.length,d,
d = jQuery.Deferred(),
p=d.promise();
while(++i<len){
p=p.then(fn[i]);
}
p.then(function(){
console.log("done");
},
function(){
console.log("Failed");
});
d.resolve();
//instead of the loop doing the following has the same output
//p.then(a).then(b).then(c);
//d.resolve();
}
test();
输出是:
a
b
status in c: pending
c
done
status in a: pending
status in b: pending
预期输出:
a
status in a: pending
b
status in b: pending
c
status in c: pending
done
尝试了以下修改的一些组合:
d = jQuery.Deferred();
setTimeout(function(){d.resolve();},100);
var p=d.promise();
while(++i<len){
p.then(fn[i]);
}
但是所有的结果都是一样的,b 在 a 的 deferred 被解决之前被调用,c 在 b 的 deferred 被解决之前被调用。
【问题讨论】:
标签: javascript jquery asynchronous promise synchronous