0

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);
        }
    });
}

enter image description here

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

  • 1
    Possible duplicate of [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – str Nov 14 '17 at 10:10

1 Answers1

1

Call it in a recursive async way:

function callService(wordIdIndex, callback) {
    services.WordExampleService.getWordExamples(wordId[wordIdIndex], function(err, response) {
        responses.push(response);

        wordIdIndex++;
        if (wordId[wordIdIndex]) {
            callService(wordIdIndex);
        }
        else if (callback) {
            callback(responses);
        }
    });
}

 callService(0, function(responses) {
     console.log(responses);
 });

Use this code instead of the second for statement.

Remember, as your requests are async, everything which relies on responses should be executed inside that callback for now on.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • Ur code is working the problem is I want to return the `responses` array and and capture the `callservice` function returned value and I have to iterate the `reponses` array. the for loop is executing before the function `callservice` – akshaya rathinavel Nov 15 '17 at 06:26
  • @akshayarathinavel Ok, chcke the update. I have added a callback. – DontVoteMeDown Nov 15 '17 at 10:41
  • I will update my question one more time I have a problem with the service call. Please have a look into it. – akshaya rathinavel Nov 16 '17 at 11:42
  • @akshayarathinavel that is because the request is **async**. You should take a research about it to understand the concept, that is why you are so confused. As I said in the last paragraph in my post, **everything** you need to do with `responses` **have** to be inside that callback. – DontVoteMeDown Nov 16 '17 at 12:31
  • yeah, I know it is **async** , See my question I have changed. But I don't want to do anything inside the callback. I want the `responses` outside the callback. Is there any way or method to do – akshaya rathinavel Nov 17 '17 at 08:50
  • @akshayarathinavel no my friend, there is not way to do so. The responses will be available **only** inside the callback. cheers. – DontVoteMeDown Nov 17 '17 at 09:49
  • 1
    Thanks for your reply – akshaya rathinavel Nov 20 '17 at 07:18