2
我正在使用Google Analytics Management API for JavaScript访问Google Analytics(分析)。如何等待Google客户端库中的所有承诺解决方案
我试图实现以下目标:从所有帐户
- 载荷过滤器(IDS数组)。
- 删除所有加载的过滤器(来自加载的帐户)。
- 完成所有删除操作后:向所有帐户添加新过滤器。
- 所有添加操作完成后:显示完成消息。
我因为第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.');
});
});
我只能访问谷歌客户端库和jQuery,但它似乎$.when.apply($,promises).then(...)
从jQuery的,这可以等待对于所有可以解决的承诺,不适用于GCL承诺对象。
能 “$ http.pendingRequests.length == 0;!”帮助你检查所有的承诺是否被退回? –
也许您正在寻找[批量请求](https://developers.google.com/api-client-library/javascript/features/promises#batch-requests)?或者,在单个承诺上调用'$ .when(promise)'应该创建一个与'$ .when'数组调用兼容的延迟jQuery。 – Bergi
'$ .when(googleRequestPromise)'不像jQueryPromise那样工作。至少我没有得到它的工作。 我没有'批量请求'来处理嵌套'.then'调用,比如'.req.then(...)。then(... req2.then()...)''。 – ddofborg