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 REST APIs with Express Getting to Know REST APIs GET a Quote, GET all Quotes

Async callback and the data declaration

Hi, I am a bit confused. The const data variable gets declared after the app.get() method is called. Because the const keyword is used it won't get hoisted. Is this possible because the callback function gets placed on the call stack and will be executed after the data variable is already assigned?

1 Answer

Steven Parker
Steven Parker
231,248 Points

The "app.get" call registers the request handler but it does not invoke it. So what's important is that "data" gets defined before a request is handled.

To be absolutely certain of this, it might have been a good idea to define "data" before calling "listen", but it wasn't at all possible back when "app.get" was called.