JavaScript Node.js Basics Building a Command Line Application Making a GET Request with https

Philip Bradbury
Philip Bradbury
33,368 Points

Why does the call back function run when not called only assigned?

Im viewing this video a little confused. The Https.get() is assigned to request. request is not called anywhere in the app.js it in only assigned. With this been the case why does the callback execute the console.dir() inside the callback? Shouldn't this only run when the request is called?

1 Answer

Steven Parker
Steven Parker
171,227 Points

The request variable is not a function.

When you say "request is not called anywhere..." it sounds like you are expecting request to contain a reference to a function, but it does not. In the assignment of request, https.get() is called, and it is the result of that call (and not the method itself) that is stored in request. The result is an http.ClientRequest object.

Just calling the https.get() method sets the callback to run when the request completes. So there's no need to make use of request just for this to happen.

Philip Bradbury
Philip Bradbury
33,368 Points

Hi, thank you for the explanation. One more question, why assign the https.get() to the request variable? thanks again

Steven Parker
Steven Parker
171,227 Points

It's done here just to be able to list out the contents with console.dir. But the http.ClientRequest object contains a lot of information about the request that might be useful in a more developed program.