Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,000 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
216,000 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.