6

I don't understand why code continues to run even after return and res.send() was called. This is a GIST to help to understand.

UPDATE:

Well, after help of community now discovery and understand that the problem is that return res.send(); occur async, in parallel of console.log().

In this specific case, the solution is wrap inside if/else.

Thanks to @Tom and @PaulPro!

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
Thales P
  • 371
  • 1
  • 6
  • 16
  • 6
    Does it still say `Hi!` if you change that to `Hi2!`? (Only to be 100% sure you do not have a stray `Hi!` anywhere else …) – user13500 Feb 12 '14 at 22:33
  • Yes! I really don't understand why. It's not very important to me, but at some point impact my code. I really expected that nothing keep running after it – Thales P Feb 12 '14 at 23:01
  • 2
    If *"Yes, it still say `Hi!`"* then you are probably not running the same file as you are editing – or you have that `Hi!` from somewhere else. – user13500 Feb 12 '14 at 23:11

2 Answers2

16

The Hi! text is printed on the console if you use following code snippet (note there is no return next to res.send).

app.get('/asd', function (req, res) {
  res.send('OK');
  console.log('Hi!');
});

Following code snippet will not print Hi! on the console as the request handling function ends along with res.send('OK'); expression

app.get('/asd', function (req, res) {
  return res.send('OK');
  console.log('Hi!');
});

The reason why you can see Hi! in the first instance is that the res.send function is performed asynchronously i.e. method sending response is queued in the JavaScript event loop and the request handling function continue execution and calls console.log with Hi! argument.

I hope that will help.

Tom
  • 26,212
  • 21
  • 100
  • 111
  • Sorry @Tom but it still print 'hi' even with return. – Thales P Feb 12 '14 at 23:10
  • @CodeWarrior, in that case there must be another piece of code in your application (maybe somewhere with `app.use`) which also performs `console.log('Hi!')`. Can you please change `'/asd'` handler and modify `Hi!` to, say, `Hi! in /asd handler` to check if that changed text will be printed out. – Tom Feb 12 '14 at 23:15
  • look at this gist: https://gist.github.com/thalesfsp/80fb332ee671e67fa0b4 this is the code. I don't have app.use. :) – Thales P Feb 12 '14 at 23:29
  • @CodeWarrior That Gist is completely different from what you posted in your question. You're not even returning from the same function that you log `Hi!` in. You're returning from an anonymous function. In fact the `console.log('Hi')` happens before the anonymous function is called. – Paul Feb 12 '14 at 23:34
  • @Paulpro no, console.log() is inside of app.get() scope. After the return res.send() its should not run. – Thales P Feb 12 '14 at 23:39
  • @CodeWarrior, in the example, there are two functions executed before request handler: `middlewares.isAuthenticated` and `middlewares.validateReqBodyAgainst`. Also you are not using `return` along with `User.findOne` so surely it looks like the first code snippet. It would be case #2 if you used `return User.findOne ...`. – Tom Feb 12 '14 at 23:40
  • @CodeWarrior The `return` is from the anonymous function `function (err, doc) {`. You don't have any return's in the function `function (req, res) {`. If you add a return right before that console.log, it won't log. – Paul Feb 12 '14 at 23:41
  • @Paulpro, i will edit the GIST to better understand, wait – Thales P Feb 12 '14 at 23:43
  • 1
    @CodeWarrior Maybe this will help you understand: http://jsfiddle.net/f7TV3/ Your GIST is similar to that. Sure you have a return somewhere in the code before `console.log`, but it's in a different function, and it gets executed after the `console.log` does. – Paul Feb 12 '14 at 23:44
  • @Paulpro my last try ;) i update the GIST. – Thales P Feb 12 '14 at 23:46
1

Well, after help of community now discovery and understand that the problem is that return res.send(); occur async, in parallel of console.log(). In this specific case, the solution is wrap inside if () {} else {}. Thanks to @Tom and @PaulPro!

Thales P
  • 371
  • 1
  • 6
  • 16