I'm current using the Q promise library in a Node/amqp app. I've read that the performance of Q vs libraries like BlueBird or Vow is... not so good.
Unfortunately, I can't figure out how to use BlueBird (or Vow) to replace my current Q usage patterns.
Here's an example:
this.Start = Q(ampq.connect(url, { heartbeat: heartbeat }))
.then((connection) => {
this.Connection = connection;
return Q(connection.createConfirmChannel());
})
.then((channel) => {
this.ConfirmChannel = channel;
channel.on('error', this.handleChannelError);
return true;
});
I should've mentioned - I'm using TypeScript... In this example I'm taking an amqplib promises, and creating a Q promise out of it (because I don't like the amqplib promises). How do I do that with BlueBird or Vow?
Another example is:
public myMethod(): Q.Promise<boolean> {
var ackDeferred = Q.defer<boolean>();
var handleChannelConfirm = (err, ok): void => {
if (err !== null) {
//message nacked!
ackDeferred.resolve(false);
}
else {
//message acked
ackDeferred.resolve(true);
}
}
...some other code here which invokes callback above...
return ackDeferred.promise;
}
How is that pattern implemented?
So my general questions is:
Yes, Bluebird is two orders of magnitude faster than Q, and is much more debuggable. You can look at the benchmarks yourself.
As for the code:
Q()
maps to Promise.resolve
in Bluebird (just like in ES6 native promises).*Q.defer
maps to Promise.defer()
although it's a better option to use the promise constructor or automatic promisification as explained in this answer. In short, the promise constructor is throw safe.Note* - once you've cast the promise, it'll assimilate thenables from other libraries automatically - so this is perfectly fine:
this.Start = Promise.resolve(ampq.connect(url, { heartbeat: heartbeat }))
.then((connection) => {
this.Connection = connection;
return connection.createConfirmChannel());
})
.then((channel) => {
this.ConfirmChannel = channel;
channel.on('error', this.handleChannelError);
return true;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With