所以每次迭代等待異步.done(),我有功能鎖定這樣的:環路
function getMainData() {
var dfd = $.Deferred();
$.getJSON('My string that i pass',
function(result) {
if (result !== undefined) {
dfd.resolve(result);
}
})
return dfd.promise()
}
function getSpecificData() {
var dfd = $.Deferred();
var myArray = [];
for (var i = 0; i < 5; i++) {
getMainData().done(function(result) {
myArray.push(result)
dfd.resolve(myArray) //This is where I am lost.
})
}
return dfd.promise()
}
getSpecificData().done(function(result) {
console.log(result);
})
我想我知道,如果你把它們連在一起的承諾是如何工作的,但我不能讓for-loop在下一次迭代之前等待異步調用完成。
能有些幫助我嗎?
謝謝你的解決方案,這個結果不錯,而我其實是沒有考慮過遞歸方法。 – Zorken17