我有一系列的項目。對於該數組中的每個項目,我需要進行API調用。
只有在項目的所有調用完成後,我纔會繼續。
var itemProgress = [];
var promises = currentBatches.map(function(batch){
HttpWrapper.send('/api/'+batch.job_id+'/progress', { "operation": 'GET' })
.then(function(result) {
batch.succeeded_time_pct = result.succeeded_by_time_pct; // I add one property to each of the item
itemProgress.push(batch); // I push it to a new array
},function(errorResponse) {
console.log(errorResponse);
});
});
在這裏,我試圖讓an API call for each of the items
後添加new property
到每個項目。
而且當所有的電話完成後,我想分配this new array to the current array
。
$q.all(promises).then(function(result){
currentBatches = itemProgress;
});
我在做什麼錯?
爲什麼currentBatches = migrationProgress; inside $q.all
正在評估之前爲每個項目執行最上面的塊。我該如何解決它?
您需要在地圖調用中使用return語句才能開始。你正在創造承諾,但你沒有回報。 – matmo