1

I am having trouble between these two bits of code. They produce different results:

thing.save()
  .then(x => res.send(x))

and

thing.save()
  .then(res.send)

The bottom example produces an error. I assume Express (where 'res' is used) is using '.this' behind the scenes, and the different formats are screwing with it. But why?

I guess the question is, what could the difference be between providing an arrow function with a single function call as a callback to something, versus providing just the function as the callback?

My understanding is that the arrow function would get called, and the inner function would be called in the same context that the arrow function is called. So calling the function directly would seem to be identical? That makes it seem like the two formats would act the same.

Matt Broatch
  • 710
  • 4
  • 16
  • Because `res.send` is a function with no context (no `this`). There are many questions here on SO about this issue. –  Sep 26 '16 at 03:40
  • I was under the impression that when a function is called as an object method, its 'this' refers to the object. Wouldn't res.send()'s 'this' be res in both cases? – Matt Broatch Sep 26 '16 at 07:20
  • But you're not saying `res.send()`. You're just saying `res.send`. –  Sep 26 '16 at 07:58
  • Huh, I guess I'm just assuming that the function you pass in as a callback gets called by the function that the callback goes into. How does it actually work? – Matt Broatch Sep 26 '16 at 21:31

0 Answers0