我使用Google Analytics Management API for JavaScript访问Google Analytics。
我试图实现以下目标:
我在第3点和第4点陷入困境。我无法等待承诺清单再次完成。
半伪代码如下,我没有waitForAllPromises(listOfPromises)
:)
var promises = [];
for(accountId in accountIds) {
(function(accountId){
promise =
loadAccountFilters(accountId) // returns gapi.client.analytics....then()
.then(function(){
deleteAccountFilters(accountId); // returns gapi.client.analytics....then()
});
promises.push(promise);
})(accountId);
}
waitForAllPromises(promises)
.then(function(){
console.log('All delete operation done.');
var promises = [];
for(accountId in accountIds) {
(function(accountId){
promise = addNewFilters(accountId); // returns gapi.client.analytics....then()
})(accountId);
}
promises.push(promise);
waitForAllPromises(promises)
.then(function(){
console.log('All add operation done.');
});
});
我只能访问Google客户端库和jQuery,但似乎来自jQuery的$.when.apply($,promises).then(...)
,它可以等待所有承诺解析,不能与GCL承诺对象一起使用。
答案 0 :(得分:0)
永远不要谈论Promise / $ q?你的逻辑很好!您应该将Promise.all / $ q.all替换为waitForAllPromises
var promises = [];
for(accountId in accountIds) {
(function(accountId){
promise =
loadAccountFilters(accountId) // returns gapi.client.analytics....then()
.then(function(){
deleteAccountFilters(accountId); // returns gapi.client.analytics....then()
});
promises.push(promise);
})(accountId);
}
Promise.all(promises)
.then(function(outputs) {
console.log('All delete operation done.');
var promises = [];
for(accountId in accountIds) {
(function(accountId){
promise = addNewFilters(accountId); // returns gapi.client.analytics....then()
})(accountId);
}
promises.push(promise);
Promise.all(promises)
.then(function() {
console.log('All add operation done.');
});
});