The correct way to bind object to Promise.then() argument [duplicate]
So I attached a
.bind
to the function and it worked. Is this the correct way to do this with bluebird?
Yes, that is one way to retain the context. You can also pass an anonymous function (you might already know this).
Promise.resolve("cherry") .then(function (value) { return chair.build(value); }) .then(console.log);
Or is there some better way?
You can actually use bluebird's Promise.bind
method, like this
Promise.resolve("cherry") .bind(chair) .then(chair.build) .then(console.log)
Now, whenever the Promise handlers (fulfill handlers or rejection handlers) are called, inside the function, this
will refer the chair
object only.
Note 1: In this specific case, console.log
also gets this
as chair
object, but it still works fine, because, in Node.js, console.log
function is defined not only on the prototype bu also on the object itself, bound to the console
object. The corresponding code is here.
Note 2: If different handlers need different contexts, then better write anonymous functions. In that case, Promise.bind
will not help much. But if you choose to use it, then you have to use different contexts for each of the handlers and your code might look something like this
var chair1 = new Chair("red")var chair2 = new Chair("green")Promise.resolve("cherry") .bind(chair1) // Changing the binding to `chair1` .then(chair1.build) .tap(console.log) .bind(chair2) // Changing the binding to `chair2` .then(chair2.build) .tap(console.log);