有没有实现无极里面angular.foreach一个可能的方法是什么? [英] Is there a possible way implementing Promise inside angular.foreach?

查看:131
本文介绍了有没有实现无极里面angular.foreach一个可能的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 var sanitizedresult=[];
    angular.forEach(voteResult, function(value, key) {
      // console.log(value.poll_watcher_id);
      var param={
        id : value.candidate_id
      }
      CandidateService.getCandidatInfo(param).then(function(success){

        // console.log(success);
        var row = {
                    ballot_name: success,
                    vote_count: value.count
                  }
                  console.log(row);
        sanitizedresult.push(row);
      },function(fail){
        console.log(fail);
      });
 });

我怎样才能延缓循环迭代为它等待承诺的响应

How can I delay the iteration of the loop for it to wait the response of the promise

推荐答案

我想你会写自己的功能循环,而不是使用angular.forEach。

I think you will have to write your own function to loop rather than using angular.forEach.

这是这里使用递归完成

This is done using recursion here

喜欢的东西如下:

var sanitizedresult=[];

 var processVotes = function(voteResult,idx){
        var param={
            id : voteResult[idx].candidate_id
          }
      CandidateService.getCandidatInfo(param).then(function(success){

        // console.log(success);
        var row = {
                    ballot_name: success,
                    vote_count: value.count
                  }
                  console.log(row);
        sanitizedresult.push(row);
        if((idx+1) < voteResult.length){
            processVotes(voteResult,idx+1);
        }else{
           //use sanitizedResult here -- maybe a function call
        }
      },function(fail){
        console.log(fail);
        if((idx+1) < voteResult.length){
            processVotes(voteResult,idx+1);
        }else{
           //use sanitizedResult here -- maybe a function call
        }

      });   
 }
 processVotes(voteResult,0);

这篇关于有没有实现无极里面angular.foreach一个可能的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆