Return Value on a callback, Javascript -


i'm having troubles using nodejs function. want return value. when @ consolelog, can see 'undefined'.

i know because callbacks not end before console.log executed dont have clue how resolve problem.

var info = api.getbridge(); console.log(info)  api.getbridge = function () {     var hue = require("node-hue-api");    var resultado;     hue.nupnpsearch(function (err, result) {       if (err) throw err;       return result;    }); } 

you try do console.log inside callback:

var info = api.getbridge(); 

but inside callback:

var callbackvalue = hue.nupnpsearch(function(err, result) {     if (err)          throw err;     return result; }); console.log(callbackvalue); 

well, know function returns. use "returnvalue" further have call code processing results , call according value:

var thisprocessesyourresults = function(somevaluetoprocess){...} 

and again, inside callback:

thisprocessesyourresults(callbackvalue); 

when working asynchronously, can not return value original calling context. resulting code (if gets complex enough) can misleading or confusing @ best. way deal promises.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -