Few questions, does Q have a bind()
method as seen in BlueBird API and this slide JavaScript Promises ~Kerrick Long? Is it part of the Core Promise/A+ Spec? If not, is there a polyfill or workaround for it?
Well, the Promises/A+ spec only specifies how .then
works, so .bind
is not a part of the Promises/A+ spec. In fact Promises/A+ specifies that the this
value behaves like a normal function call and promises are not bound - Bluebird gets around this limitation by returning a BoundPromise which is not a regular promise and interops seamlessly with other promise libraries.
There is no equivalent Q feature, once ES6 lands you'll be able to use the "fat arrow" =>
in order to lexically bind the scope in then
chains:
this.x = 15;
Q().then(() => console.log(this.x)) // always logs 15
Generally speaking, this is one of many features Bluebird has over Q, there is no obvious way to 'shim it in' since it changes the behavior of .then
- you'd have to override .then
in Q promises and effectively create a wrapper over the whole library to accomplish that.