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
Post a Comment