javascript - Learnyounode, functions with callbacks - Juggling Async -


im very new node.js, javascript in general, , functional programming (which node if im not mistaken?)

im on stage of doing learnyounode tutorials. know can find solutions , work out fine, im little curious why wouldnt code work...

if familiar learnyounode im stuck @ "juggling async".

the code wrote:

var http = require("http");  var addriee = [process.argv[2], process.argv[3], process.argv[4]];  function getstufffromnet(address, callback) {   http.get(address, function getshitdone(response) {     var datatocallback = "";     response.on("error", function(data) {       callback(data, null);     });      response.on("data", function(data) {       datatocallback+=data;     });      response.on("end", function(data) {       callback(null, datatocallback);     });    }); };  function printtoconsole(data) {   console.log(data); }  printtoconsole(getstufffromnet(addriee[0])); 

my goal reuse function "stuff net", error is:

learnyounode run http-get3.js  undefined  /home/ubuntu/workspace/learnyounode/http-get3.js:17       callback(null, datatocallback);       ^ typeerror: undefined not function     @ incomingmessage.<anonymous> (/home/ubuntu/workspace/learnyounode/http-get3.js:17:7)     @ incomingmessage.emit (events.js:117:20)     @ _stream_readable.js:944:16     @ process._tickcallback (node.js:442:13) 

why last callback null , not data ? might handier not initialize

var datatocallback = "";

to

var datatocallback;

because else can't use data

typeof datatocallback !== 'undefined'

not sure 's atm.

also try comment you're code lot more. when you're learning it.

example of debugging level have (noob or not find errors way)

        /**          * divest desired amount          */         socket.on("divest", function (amount) {             error.debug(classname + "divest called [" + amount + "]"); 
            invest.divest(hash, amount, function (err, callback) {                 if (!err) {                     error.debug(uid, name + "  />divesting [cback]" + callback);                 } else {                     error.debug(uid, name + "  />divesting [error]" + err);                 }                 socket.emit("done", true);             });         }); 

hope helped.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -