2016-05-03 12 views
0
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); 
     }); 
}); 

Wie kann ich die Iteration der Schleife verzögern sie die Antwort des VersprechenGibt es eine Möglichkeit, Promise in angular.foreach zu implementieren?

+0

Mögliches Duplikat von [auf async-Funktion warten in Schleife, um die Ausführung vor der nächsten Iteration zu beenden] (http://stackoverflow.com/questions/32740236/wait-for- async-function-in-loop-to-finish-executing-vor-next-iteration – Maverick

Antwort

1

Ich denke, zu warten, werden Sie Ihre eigene Funktion Schleife schreiben müssen, anstatt angular.forEach verwenden.

Dies wird durch Rekursion here

getan

So etwas wie unten:

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); 
+0

Ich überprüfe die Linked Sie gab, wie kann ich diesen Code zu meinem Code implementieren Ich bin nur ein Anfänger zu diesem Framework, – vin

+0

aktualisiert die Antwort mit Beispielcode – Maverick

+0

die Ausgabe sagte, dass die candidate_id nicht definiert ist, genau wie meine erste Probe, wenn ich den Code replizieren – vin

0
var sanitizedresult=[]; 

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

    // console.log(success); 
    console.log(idx+"Thi is it"); 
    var row = { 
       ballot_name: success, 
       vote_count: voteResult[idx].count 
       } 
       console.log(row); 
    sanitizedresult.push(row); 
    if(idx < voteResult.length-1){ 
     processVotes(voteResult,idx+1); 
    } 
    },function(fail){ 
    console.log(fail); 
    if(idx < voteResult.length-1){ 
     processVotes(voteResult,idx+1); 
    }  
    }); 
} 
processVotes(voteResult,0); 

console.log(sanitizedresult); 

Es gibt keine Fehler, aber immer noch die sanitizedresult ist leer, was die mögliche Ursache sein, warum die „sanitizedresult“ ist leer

Verwandte Themen