You are reading a single comment by @DrAzzy and its replies. Click here to read the full conversation.
  • How does the callback in an http request work? Why is there a second callback? I thought the callback supplied with the request would be called when the response was returned, yet this seems to be placing another callback waiting on... something...?

    var http = require("http");
    http.get("http://www.espruino.com", function(res) {
     res.on('data', function(data) {
      console.log(data);
     });
    });
    

    When does res.on() get called?
    Does res.on('data') get called only once per request, or can it be called multiple times, each time with a piece of the data? If the latter is true, how do I know that i've got all the pieces?

    The link in the http docs for more info on the node.js http api is dead - possibly when working this would explain it - but nodemanual.org seems to be down. http://nodemanual.org/latest/nodejs_ref_­guide/http.html

    The use case I've got in mind is - I want to fetch a file and evaluate it, and am wondering if I can safely do this, or whether sometimes the data won't be complete, hence the eval will fail.

    var http = require("http");
    http.get("http://master/program.js", function(res) {
     res.on('data', function(data) {
      eval(data);
     });
    });
    
About

Avatar for DrAzzy @DrAzzy started