Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多重查询以及如何等待最后一次查询并执行smth?_Javascript_Angularjs_Underscore.js - Fatal编程技术网

Javascript 多重查询以及如何等待最后一次查询并执行smth?

Javascript 多重查询以及如何等待最后一次查询并执行smth?,javascript,angularjs,underscore.js,Javascript,Angularjs,Underscore.js,我有很多疑问: angular.forEach(已检查,功能(项目,i){ updateData({id:Math.floor(item.id),prcn:item.value}); });您可以使用 现场示范 角度模块('ExampleApp',[]) .controller('ExampleController',函数($scope,$q,$timeout){ $scope.checkeds=[1,2,1,2,1,3,1]; 函数更新数据(val,ind){ //模拟调用服务器端$htt

我有很多疑问:

angular.forEach(已检查,功能(项目,i){
updateData({id:Math.floor(item.id),prcn:item.value});
});您可以使用

现场示范

角度模块('ExampleApp',[]) .controller('ExampleController',函数($scope,$q,$timeout){ $scope.checkeds=[1,2,1,2,1,3,1]; 函数更新数据(val,ind){ //模拟调用服务器端$http.post,返回$promise var defered=$q.defer(); $timeout(函数(){ log('executed',val',index',ind); 延迟。解决(); },val*1000); //在您的情况下,需要return clientService.updateOptimizable(model); 延期归还。承诺; } $scope.start=函数(){ $scope.promissions=[]; 角度.forEach($scope.checkeds,函数(项,i){ $scope.promises.push(updateData(item,i)); }); $q.all($scope.promissions)。然后(function(){//调用此函数,然后解析所有更新数据(已完成) console.log('ALL executed'); }); } });

开始

最后一个查询来自哪里?最后一个查询依赖于angular.foreach或updateData函数。@oseintow,最后一个查询依赖于updateData,即在最后一个执行updateData函数后执行read()谢谢!
// let set a flag to monitor our last item in the loop
var flag = false;
angular.forEach(checkeds, function(item, i){
   if(i == checkeds.length - 1){
      flag = true;
      updateData({id: Math.floor(item.id), prcn: item.value });
   }
});


function updateData(model) {
   clientService.updateOptimizable(model).then(function(response){
     // if flag is true then do something
     if(flag == true){
        read(); // maybe like this
     }
   }
}