一旦数组的所有always
元素都完成(成功或失败),我就试图运行一些代码。
您可以在此处查看完整的代码:http://jsfiddle.net/Lkjcrdtz/4/
基本上,我希望从这里获得$.when
.apply(undefined, reqs)
.always(function(data) {
console.log('ALL ALWAYS', data);
});
钩子:
ALL ALWAYS
在堆积在那里的所有请求成功或失败时运行。当前,您可以在控制台中观察到void TreeSet::inorderRec(AVLNode *root, int *arr, int pos) {
// lets be pos = 0
if (root) {
inorderRec(root->left, arr, pos); // call with pos = 0
arr[pos++] = root->key; // write to arr[0], then set pos = 1
inorderRec(root->right, arr, pos + 1); // call with pos = 2
}
}
早已登录。
答案 0 :(得分:0)
现代浏览器的一个简单解决方案是将更新的fetch()
API与Promise.all()
一起使用
var makeReq = function(url, pos) {
var finalUrl = url + pos;
// intentionally make this request a failed one
if (pos % 2 === 0) {
finalUrl = "https://jsonplaceholder.typicode.com/423423rfvzdsv";
}
return fetch(finalUrl).then(function(resp) {
console.log('Request for user #', pos);
// if successful request return data promise otherwise return something
// that can be used to filter out in final results
return resp.ok ? resp.json() : {error: true, status: resp.status, id: pos }
})
};
// mock API
var theUrl = "https://jsonplaceholder.typicode.com/users/";
var reqs = [];
for (var i = 1; i <= 5; i++) {
reqs.push(makeReq(theUrl, i));
}
Promise.all(reqs).then(function(results) {
console.log('---- ALL DONE ----')
// filter out good requests
results.forEach(function(o) {
if (o.error) {
console.log(`No results for user #${o.id}`);
} else {
console.log(`User #${o.id} name = ${o.name}`);
}
})
})