0

I have to check a string with more strings through api, I am using request module for get Request. The api request is stored in an array, after completion of all requests i am using callback to send prepared list. but I am getting empty list from for loop. Where I am going wrong? Please tell me.

var list = ['Mobiles with best', 'what is a processor', 'what do you mean by', 'show me poular phones', 'show me best phones', 'Show me the phones with best', 'phones better than', 'show me thephones released', 'where can i find', 'what is the lowest price of', 'Why are mobiles ranked this way?', 'Why only these features are considered for ranking?', 'can you tell me moreabout', 'Do you know anything about', 'What are the highlights of', 'Why is', 'How good is', 'How good are the reviews of', 'What is great about', 'What are people talking about', 'Give me the reviews of', 'What is the logic ?', 'What is the basis of your ranking?', 'How did you rank them?', 'how did you shortlist this?', 'help me understand the scores better',
'Show me models better than', 'Show me devices more advanced when compared to', 'Give me mobiles with better specs than', 'what is the best phone in the list?']

    var phrase1  =" mobiles with best";

    function find(callback) {
        var semantic_list = []
        for (var i in list) {
            phrase1 = querystring.escape(phrase1)
            var phrase2 = querystring.escape(list[i])
            var query_string = "http://swoogle.umbc.edu/SimService/GetSimilarity?operation=api&phrase1=" + phrase1 + "&phrase2=" + phrase2;
            request(query_string, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    //console.log(body) // Show the HTML for the Google homepage.
                    semantic_list.push(body)
                }

            })
            console.log(semantic_list)

            if( i == list.length -1){
                console.log(semantic_list)
                callback && callback(semantic_list)
            }
        }

    }

    find(function(data){
        "use strict";
        console.log(data);
    })
Voora Tarun
  • 1,266
  • 1
  • 20
  • 38
  • 1
    `if( i == list.length -1){` runs immediately after each request has been scheduled, but before any of them will have been completed. – OrangeDog Jun 06 '16 at 12:55
  • 1
    Possible duplicate of [Node.js: When do you know when a collection of asynchronous tasks is done?](http://stackoverflow.com/questions/6907517/node-js-when-do-you-know-when-a-collection-of-asynchronous-tasks-is-done) – OrangeDog Jun 06 '16 at 12:58
  • can anyone modify the above code ? – Voora Tarun Jun 06 '16 at 13:06
  • We're not here to write code for you. See the linked and related questions for what you should be doing, try again yourself, then ask another question later if required and on-topic. – OrangeDog Jun 06 '16 at 13:29
  • @VooraTarun simplify your snippet, nobody wants to get its hand in something so cluttered and it wouldn't be benefiting anybody else. – TKrugg Jun 06 '16 at 14:48

1 Answers1

1

The request function puts the data into semantic_list inside a callback. The callback is not executed until after the request returns (a long time from a computer's perspective). The rest of the code will have finished running, including your calling the callback function well before any data has had a chance to make it into semantic_list.

Your best bet would be to use one of the many modules that help you with running multiple parallel things. Two good, yet very different, choices include async.js and bluebird.js.

memimomu
  • 161
  • 4