this question has answer here:
- how return response asynchronous call? 21 answers
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
Post a Comment