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 User Authentication With Express and Mongo Improving the App with Custom Middleware Writing Custom Middleware

Brendan Moran
Brendan Moran
14,052 Points

Why all the return statements?

I just looked at the express documentation and saw that they don't have all the return statements where the instructor has put them. Is this some kind of reliability/performance fallback, or is the need for the return statement a thing of the past? I'm assuming the return statements are there because that's how it used to be done. Correct me if I am wrong; I want to know if there is some good reason to be using them.

3 Answers

Tom Geraghty
Tom Geraghty
24,174 Points

Because sometimes you do need to exit the current function immediately; the return just guarantees no other code in that block is executed while the app is managing the callback.

I imagine Dave is using it everywhere here because it is optional and having it there always will ensure it is there when it needs to be and that he is being consistent in the code he writes since that consistency will be learned by all of us. It's likely more distracting to students if he were to be inconsistent and sometimes put return and other times not.

Brendan Moran
Brendan Moran
14,052 Points

Thanks Tom, that's the best answer for me. To prevent any asynchronous code from running in the current block. Makes sense.

akak
akak
29,445 Points

As you correctly noticed you do not have to return from, for example, res.send() but adding return won't hurt either.

Have in mind that there might be a rare situation when you DO want to return. For example this code

router.get('/login', mid.loggedOut, function(req, res, next) {
  res.send('Hello there');
  console.log('Im still here');
});

could output console.log message to the console. It's because res.send is asynchronous and can finish after the next statement. So writing return res.send is like a short-circuiting - you make sure no other code will execute after it. You usually don't need that. Until that one case you do :)

Brendan Moran
Brendan Moran
14,052 Points

Makes sense. I know it can't hurt to put them there, but I am wondering what the compelling reason is for having return statements where Dave has them in his code. They are being used before every single function-exiting call to another function, even if that call is the last executable statement in the function and even if it is not asynchronous. E.g. return next() or return next(error) or return res.redirect('somewhere') as the last statement. Like this:

function redirLoggedInUser (req, res, err) {
  if (req.session.userId) return res.redirect('/profile')
  else return next()
}

//but I see no reason not to write it as follows. Am I wrong?

function redirLoggedInUser (req, res, err) {
  if (req.session.userId) res.redirect('/profile')
  else next()
}

I understand why you might use the return statement in your example, but the way he is using it is different. Your answer is helpful, but I am still wondering: does this "return everything" approach to writing express belong to an older version of express? Or is it, like in your example, serving some good purpose in the asynchronous flow of things?

Typically return is pretty self explaining. It returns to the caller. So when you go to the route http://www.google.com/ there server returns the view to the client (your computer). I wouldn't say this has a performance issue.

Other uses of return are in functions or ES6 classes. Your code to call a function and get the response returned from the function. ES6 classes can return data when there methods are called.

I'm not 100% if it's going to cause some huge issue if you don't use return but best to follow the video.

Brendan Moran
Brendan Moran
14,052 Points

I understand what a return statement does. What I am wanting to understand is the consistent use of them in the course when the documentation does not call for them.