Awesome trick seen on mongoose doc:
db.on('error', console.error.bind(console, 'connection error:'));
The
bind()
method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
so the above is equivalent to console.log with first argument set to 'connection error:', so when errors it prints something like
connection error: { [MongoError: getaddrinfo EIO] name: 'MongoError', message: 'getaddrinfo EIO' }
more tricks using bind