2

In this Blog article: "We have a problem with promises", I came upon this code snippet:

somePromise().then(function () {
  return anotherPromise();
}).then(function () {
  return yetAnotherPromise();
}).catch(console.log.bind(console)); // <-- this is badass

What puzzles me here is the last line, the 'badass' one. I had never had to use the 'bind' method anywhere up until now and am not really knowledgable in JavaScript. Having now just read about the bind method in the Mozilla Developer Network docs, I understand that one can assign a different "this" object, to any function with it (and also hard-insert predefined arguments, so that they become 'non-arguments' to the returned function).

However, as far as I understand, console.log.bind(console) should be simply the same as console.log. What is the difference? What have I missed or misunderstood here, and why is this kind of construct used?

Thanks.

trollkotze
  • 707
  • 7
  • 17
  • 2
    When you detach it from `console`, i.e. `var log = console.log;` and then call it as `log('derp');`, `this` will no longer refer to `console`. `bind` fixes that. – Siguza May 20 '17 at 15:49
  • 1
    Duplicate question. check http://stackoverflow.com/questions/28668759/what-does-this-statement-do-console-log-bindconsole – A Paul May 20 '17 at 15:51
  • Thanks for explaining the gist @Siguza and for pointing to the already answered question @A Paul. Very helpful. – trollkotze May 20 '17 at 21:37

0 Answers0