javascript - Use data from Node.js function -


this question has answer here:

i learning work nodejs , came problem. getcurrentweather() asynchronous function loads instantly when start app, , writes data variables. when use variables outside function console.log() data, undefined results, because node doesn't wait until getcurrentweather() gets api data, executes code instantly while still doesn't have return. solved function renewcurrentweather() , added settimeout wait while getcurrentweather() gets data, , console.log() it.

this method works me, problem if want use data more 1 time,i have use function wiht settimeout. looks me little buggy, because need use data in more complex situations.

so question is, how make node.js execute console.log(temp, cond) then, when getcurrentweather() finished loading data api. in other words, want use variables temp , cond everywhere in app, without funtions attached it.

// weather data weather underground every 3 minutes (due daily 500 calls per day limit)  function getcurrentweather() { wunder.conditions('', function (err, data) {  if (err) throw err;  temp = data.temp_c;  cond = data.weather; }); };  getcurrentweather(); setinterval(getcurrentweather, 180000);  // use data recieved api times need function renewcurrentweather() {   settimeout(function() {   console.log(temp + " " + cond); }, 1000); }; 

if you're learning nodejs, need learn first asynchronous code writing. have multiple concepts do that, async callbacks or promises.

async callbacks used in nodejs. it's upon them built express, router, , middleware system.

because nodejs heavily based on async callbacks, there nodejs convention asks provide callback function last argument of async function.

function callbackexample (err[, data][, callback]) {     callback(); } 

you rewrite code this:

function getcurrentweather(callback) {     wunder.conditions('', function (err, data) {         if (err)             throw err;          // save data         temp = data.temp_c;         cond = data.weather;          // run callback function if 1 provided, , send data         if (callback)             callback(null, { temp: temp, cond: cond });     }); };  setinterval(getcurrentweather, 180000); 

you have function takes optional callback function. when want last updated data, call getcurrentweather callback function:

getcurrentweather(function (err, data) {     if (err)         throw err;      // here can want data     console.log(data.temp);     console.log(data.cond); }); 

you should read javascript event-loop.

this introduction asynchronous writing, not fit needs because it'll update currentweather each time call getcurrentweather, it's way start.

what need, execute refresh weather once each 3 minutes, have queue of registered callback functions waiting weather update.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -