5

I found it bit strange that, resolving a promise like this

.then(console.log, console.log)

doesn't work, but this works

 .then(d => {
     console.log(d);
   }, err => {
     console.log(err);
   });

what am I missing

bsr
  • 57,282
  • 86
  • 216
  • 316
  • 2
    The `log` function needs the `console` context - you lose the proper `this` reference in the first code. – Pointy Mar 03 '16 at 00:56
  • thanks.. what should I do close this qn? or delete? – bsr Mar 03 '16 at 01:00
  • Well maybe with a more specific title (like, "Weird problem using console.log as a promise callback" or something) it might be useful, and you or I can type in a brief answer. – Pointy Mar 03 '16 at 01:03
  • please write it as an answer, I will accept it then. thanks again for your help – bsr Mar 03 '16 at 01:04

1 Answers1

4

The console.log() function needs the console object as the value of this, or else it won't/can't work. You achieve that with the second bit of code because you're calling console.log() as one normally would. In the first code, however, you're "stripping" the reference to the function away from the object itself, so when the promise mechanism makes the function call it has no way of knowing that it should arrange for this to be the console object.

You could also do

.then(console.log.bind(console), console.log.bind(console))

though that's pretty ugly :)

Pointy
  • 405,095
  • 59
  • 585
  • 614