4 ms·
coroutines with generators are more powerful (cancellation, automatic resource cleanup when the generator terminates etc) and have worked for a long time out of
by esailija 11y ago
coroutines with generators are more powerful (cancellation, automatic resource cleanup when the generator terminates etc) and have worked for a long time out of the box. The only difference is that you write `yield` instead of `await` and `decorator(function*` instead of `async function`
- phpnode 11y agoBabel can transform async functions into Bluebird coroutines, so the best of both worlds - https://www.npmjs.com/package/babel-plugin-transform-async-to-module-method https://www.npmjs.com/package/babel-plugin-transform-async-t...
- esailija 11y agoYes but with this you lose the powerful features I just mentioned: automatic resource cleanup[1], cancellation, yield handlers.. don't you? 1. https://github.com/petkaantonov/bluebird/issues/1014 https://github.com/petkaantonov/bluebird/issues/1014
- phpnode 11y agoNo I think those should work with async functions as it is literally just translating `async` -> `yield`, but worth investigating.
- alfonsodev 11y agoThanks for commenting on that, I didn't think about those issues. I'll research on how much impact could have on performance. In the other hand I believe syntax is not the only difference. I'm not full time js dev, but after a quick search, I found Domenic pointing 4 differences here https://esdiscuss.org/topic/does-async-await-solve-a-real-problem https://esdiscuss.org/topic/does-async-await-solve-a-real-pr...
- esailija 11y agoThese seem to be mostly about undecorated generators. 3. Is a legit difference but it is actually a syntax error to do yield 1 + yield 2. So that is fixable in a backward compatible way.
- mtzaldo 11y agoCould you put a reference for the decorator/yield usage? Thanks
- esailija 11y agohttp://bluebirdjs.com/docs/api/promise.coroutine.html http://bluebirdjs.com/docs/api/promise.coroutine.html
- mtzaldo 11y agoThanks, I will review it.
- l1ambda 11y agohttps://github.com/llambda/koa-boiler/blob/master/app.js#L75 https://github.com/llambda/koa-boiler/blob/master/app.js#L75 is an example of Bluebird coroutines paired with Koa.