0

Learnyounode is a commandline based learn-by-doing-and-getting-your-results-tested tutorial for javascript's node

In the seventh tutorial, the idea is to make something of a http client using node.js's http's get function.

The get function has two arguments

  1. the url
  2. a callback function for the asynchronous job

So I scourgd the surface of the internet looking for a way to continously accept data till the operation ended. After a while seeing that every answer was pretty much along the lines of

      function callback(res){

           res.on("data",function (data) { console.log(data.toString());})
   }


  http.get(url,callback)

I thought maybe failing the inbuilt tests would give me a clue to see how to make multiple calls but weirdly enough it passed the multiple calls test.So I thought the test called the file and hence the function, multiple times .. but after some tries..I realised that wasn't the case.

So my question : what exactly goes on behind a async call ? how is it possible for the mechanism to call it more than once? What other surprises should I expect ? to me this is taking black box thinking to a whole new level and I place it on par with thinking about list monads atm.

5
  • What is "learnyouanode?" What programming language are you using? Commented Nov 14, 2014 at 4:21
  • @RobertHarvey oh I thought of adding those tags, but then it occured to me this has more to do with the asynchronous nature of node.JS than node.JS. Does that logic not count? Commented Nov 14, 2014 at 4:26
  • Actually, the technology and programming language being used is more important. Commented Nov 14, 2014 at 4:26
  • 1
    Of course, there's nothing that prevents the get from calling the callback function more than once. Commented Nov 14, 2014 at 4:33
  • "I place it on par with thinking about list monads atm." - Huh? If you have understood monads, and want monadic asynchronous actions, then you should look into promises (which guarantee that handlers are called only once) Commented Nov 14, 2014 at 4:49

1 Answer 1

1

I think the key point is to understand that res is an EventEmitter (IncomingMessage to be more accurate), so what does the function named callback (only once on the response event of the httpClientRequest object you create when calling http.get()) is to attach an event listener to the data event of res. Without going into details, to optimise data flow, when you receive some data bytes from the network a buffer is filled and when is full the event 'data' is triggered so you can process the incoming chunk. Hence the callback you have set to the data event is executed on every chunk of data coming from the network

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.