我做这样的事情
$.getJSON(url1, function(response1){
if(response1.status == 'success'){
$.getJSON(url2, function(response2){
if(response2.status == 'success') {
$.getJSON(url3, function(response3){
if(response3.status == 'success') {
//final code here
}
});
}
});
}
});
我听说过异步库像https://github.com/caolan/async#series 我如何在这种情况下实现这一点?
您不执行该操作!你这样做,如果你希望它看起来更干净,你使用延期和承诺做同样的事情。 – adeneo 2013-02-24 05:43:48
你拥有的代码已经是异步的,如果你想要一个更干净的代码,如@adeneo建议你可以使用不同的/ promise的API。为了测试异步性质,如果你愿意,你可以在''.getJson()'调用和'console.log('回调后')之后添加'console.log('post''后面''''if' response2.status =='success')'语句 – 2013-02-24 05:48:22
要使用上面建议的jQuery Deferreds,请查看http://api.jquery.com/jQuery.when/ – dcro 2013-02-24 05:54:15