似乎有很多关于等待ajax调用完成,或等待ajax调用完成动态数量的调用或循环的类似问题,但我似乎无法弄清楚如何将两者结合起来逻辑。
我正在尝试做这样的事情:
function getUserData(tag) {
var request = {
"jsonrpc": "2.0",
"id": Math.round(Math.random() * (999999 - 100000) + 100000),
"method": "UserAdmin.get",
"params": {
"api_key": key,
"tag_id": tag
}
};
return $.ajax({
type: 'POST',
url: '/api/v1/api.php',
data: JSON.stringify(request),
dataType: 'json'
}).then(getUserProfile, ajaxError);
}
function getUserProfile(data) {
var promises = [];
$.each(data.result, function (id, value) {
var request = {
"jsonrpc": "2.0",
"id": Math.round(Math.random() * (999999 - 100000) + 100000),
"method": "Profile.getInfo",
"params": {
"user_id": id
}
};
var promise = $.ajax({
type: 'POST',
url: 'api/v1/api.php',
data: JSON.stringify(request),
dataType: 'json'
}).then(function (response) {
var data = response.result;
var twitch = data.gamerid_twitch;
if (twitch != '') {
// This creates dom objects, but can/should be synchronous
createTwitchStreamer(twitch);
}
}, ajaxError);
promises.push(promise);
});
$.when.apply(null, promises).done(function () {
// I want to be sure everything is done before I run this; asynchronously
delegateStreamVisibility();
});
}
一切正常,但$.when(...
在完成对每个结果的调用之前报告完成。一些指导非常感谢。