WordId
is an array. I want to iterate an array and make service call and store the response in responses
array.
var wordId = [];
var responses = [];
for (var i = 0; i < result.words.length; i++) {
if (result.words[i].exampleSentences == undefined) {
wordId.push(result.words[i].identifier);
}
}
self.getWordExample(0, wordId, responses, function(responses) {
console.log(responses);
});
console.log("outside",responses);
My service file
getWordExample: function(i, wordId, responses,callback) {
var self = this;
services.WordExampleService.getWordExamples(wordId[i], function(err, response) {
responses.push(response);
i++;
if (wordId[i]) {
console.log("wordid",wordId[i]);
self.getWordExample(i, wordId, responses);
}
else if(callback){
console.log(responses);
callback(responses);
}
});
}
This is the console.Why the service is executing first and wordid is executing next
Is there any way to execute the function first and then return the responses