javascript - GET request function making Node.js code execute out of order -


this question has answer here:

it seems if code executing out of order...

here code:

console.log("function call - " + profavailable("plumber")); function profavailable(occ) {     var result = false;     var reqget = http.get(optionsget, function(res) {         res.on('data', function(d) {             //parsing data json format             var parsedshit = json.parse(d);             providers = parsedshit.providers;//getting list of providers             console.log("provider length: " + providers.length);             (var = 0; < providers.length; i++) {                 if (providers[i].occupation.localecompare(occ) == 0 && providers[i].status == 0) {                     result = true;                 }             }         });     });     reqget.end();     console.log("result - " + result);     return result; } 

my output this:

result - false function call - false provider length: 4 

my code calling function, skipping on http request, returning method false, , entering http request inside function (as can see print statements in code, , output). first time working stuff this, i'm not sure why code executing in order, because seems strange.

edit** value returned function depends on info retrieve call, how fix code incorporate that?

that's how program expected behave. need versed asynchronous programming. ref: http://rowanmanning.com/posts/javascript-for-beginners-async/


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -