Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 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
229,644 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
229,644 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.