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