4

Seems like Bluebird overlaps Co in generator/coroutine related functionality. Bluebird is espoused to have exceptional speed-performance, so for discussion sake, (assuming the aforementioned overlap premise is true) if one wanted to substitute Bluebird for Co in Koa (Node.js context), could it be easily be done without diminishing Koa's functionality, and if so how?

(My guess is it can't practically be done since it seems Koa is built over Co and doesn't explicitly expose it, but facades it. Such a substitution it seems would be tantamount to replacing jQuery with something else in Bootstrap)

JLS
  • 691
  • 1
  • 6
  • 10
  • possible duplicate of [Co.js and bluebird.js -- what's the difference?](http://stackoverflow.com/questions/22134167/co-js-and-bluebird-js-whats-the-difference) – Evan Carroll May 27 '14 at 17:47

2 Answers2

8

First of all, bluebird and co are not comparable like that. You mean Bluebird.coroutine vs co (short for coroutine).

Now, the difference between Bluebird.coroutine and co is that co only allows you to yield a certain set of hard-coded types. While Bluebird.coroutine can be configured to support yielding arbitrary types, the documentation for example contains examples how you can add support for yielding thunks and callbacks.

Async generators are so trivial that the only differences there can be between implementations is what types you can yield and how it performs. Not much room to be better or worse.

However bluebird.coroutine is only a fraction of bluebird features.

Generators only solve the problem of making a sequence of actions less verbose. There is a lot of useful functionality for more advanced needs like resource management, concurrency coordination, error handling, cancellation+timeouts and long stack traces which are impossible or extremely painful if you only have async generators powered by thunks/callbacks/minimal promises.


You can make a drop-in replacement for co by configuring all the yield types that co supports and then just using bluebird.coroutine:

var co = require("bluebird").coroutine;
// Configure all yield types you need using co.addYieldHandler
// See documentation for examples
module.exports = co;

However this doesn't really make any sense since very little code actually should run directly in your request handler - the functions that the request handler calls however do. And those functions are not helped by koa (hmm so what is the point of koa again? :D), so they can be bluebird coroutines directly.

Esailija
  • 138,174
  • 23
  • 272
  • 326
-6

esailija said this about Bluebird,

a feature is being added that allows not only yielding callbacks, thunks etc but any arbitrary thing that comes to your mind. Bluebird is also the fastest. So after this version koa should be just using bluebird indeed. See https://github.com/petkaantonov/bluebird/issues/131#issuecomment-36975495

That said, I don't believe him. And, I don't believe a bluebird wrapper would be faster than Co -- if such a thing were possible. Co.js works, and there is no way possible to get Bluebird.js to pass the tests currently. If you're using ES6, ignore Bluebird entirely and use Co.

Community
  • 1
  • 1
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • This answer is entirely false. – Benjamin Gruenbaum Aug 19 '14 at 08:17
  • You need to substantiate your opinions. What tests are you referring to? In what way would bluebird not pass them? With my own (promise heavy) code I've seen a substantial speedup through replacing native promises with bluebird, so I see no reason why co + native ES6 might not be sped up with bluebird (although co + bluebird promises might be just as fast). – Euan Smith Mar 17 '17 at 14:49