【发布时间】:2017-11-23 08:14:50
【问题描述】:
我有一系列项目。对于该数组中的每一项,我都需要进行 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 在为每个项目执行最高块之前被评估。我该如何解决?
【问题讨论】:
-
您需要在 map 调用中使用 return 语句才能开始。您正在创建承诺,但您没有返回它们。
标签: javascript angularjs arrays promise