Transfer Q to BlueBird (or Vow)

I am currently using the Q-promise library in a Node / amqp application. I read that the performance of Q libraries like BlueBird or Vow is not so good.

Unfortunately, I cannot figure out how to use BlueBird (or Vow) to replace my current Q usage patterns.

Here is 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 have mentioned - I use TypeScript ... In this example, I take amqplib promises and create a Q sentence from it (because I don't like amqplib promises). How do I do this with BlueBird or Vow?

Another example:

 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 this template implemented?

So my general questions are:

  • Are differences in performance from truth?
  • How do I switch from Q to BlueBird or Vow, focusing on these two patterns (but an explanation of using β€œthen” would be great too?)
+6
source share
1 answer

Yes, Bluebird is two orders of magnitude faster than Q, and much more debugged. You yourself can see the tests.

Regarding the code:

  • Q() displayed in Promise.resolve in Bluebird (as in ES6 native promises). *
  • Q.defer displayed in Promise.defer() , although this is the best option for using the promise constructor or automatic promise, as described in this answer . In short, the promise constructor is safe to throw.

Note * - as soon as you set aside a promise, it will automatically assimilate thenables from other libraries - this is great:

 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; }); 
+12
source

Source: https://habr.com/ru/post/969543/


All Articles