Hi there I've been trying to use promise technique to wait for the execution of calls inside a for loop.However I am not able to hold on.
"return mainArr" gives undefined
//export CSV
Loop
//Assumes data is a json object list.
function getCustomData(data){
var mainArr=[];
for (var i = 0; i < data.length; i++) {
var obj={};
obj.word = $('#type-ahead-input').val();
obj.synonym = data[i].word;
obj.similarityCount = (data[i].similarityCount).toFixed(2);
obj.emailCount = data[i].occuranceCount;
obj.synonymlist = getSynonymList(data[i].word);
mainArr.push(obj);
}
return mainArr;
}
function getSynonymList(inputWord){
return WordService.getSynonymList({
ids : inputWord
}).$promise
.then(function($response) {
var output = $response;
var wordList =[];
for (var i = 0; i < output.length; i++) {
wordList.push(output[i].word);
}
return wordList;
});
}
Factory Resource Call Signature
getSynonymList : {
method : 'GET',
isArray: true,
async : false,
url : appRoot + '/synonym',
params : {
word : '@word'
}
},
getCustomDatacan't be undefined.$promise.thenmethod results in a$qpromise, not data. Using the same name,getSynonymList, for a service that returns a$resourceobject, and a function that returns a promise makes your code confusing and difficult to understand.