I found out the hard way that one can't simply just pass in a object's function into the Bluebird then
. I'm assuming that Bluebird's then
is doing some magic and wrapping the passed in function in an anonymous function. So I attached a .bind
to the function and it worked. Is this the correct way to do this with bluebird? Or is there some better way?
var Promise = require("bluebird")
var Chair = function(){
this.color = "red"
return this
}
Chair.prototype.build = function(wood){
return this.color + " " + wood
}
var chair = new Chair()
//var x = chair.build("cherry")
Promise.resolve("cherry")
.then(chair.build.bind(chair)) // color is undefined without bind
.then(console.log)
I know none of this is async so please bare with the sync example, my usage is async.