4 ms·
Promises + ES6 generators (Promise.coroutine() in bluebird) solve all of these problems.
by peterhunt 12y ago
Promises + ES6 generators (Promise.coroutine() in bluebird) solve all of these problems.
- Arnavion 12y agoIndeed, I'm surprised the author doesn't know about them (or atleast doesn't mention them). Especially since even the async-await strawman (the first thing you'd find when googling for async-await in JS) demonstrates an implementation via generators...
- skeeterbug 12y agohttps://github.com/srikumarks/cspjs https://github.com/srikumarks/cspjs - The readme has benchmarks from bluebird generators. It is only 74 ms slower than the sweet.js solution. So the author knows.
- inglor 12y agoLook at the author's post on using promises. The author does not understand promises very well yet.
- moberemk 12y agoGenerators are up there with the let keyword in the list of "things I wish I had (native) access to already" because there's just so many use cases for them. This is definitely one combination I haven't thought of before though, so thanks for bringing it up!
- _greim_ 12y agoIs this similar to the co utility? I've been using it to yield partially-applied node calls directly, no promises required. var text = yield fs.readFile.bind(null, '/file.txt')
- inglor 12y agoYes, it's similar, `Promise.coroutine` is faster and a stronger abstraction though, especially with `addYieldHandler`.
- jc123 12y agoWill promises still be needed with generators?
- AgentME 12y agoThey work together very nicely. See taskjs (http://taskjs.org/ http://taskjs.org/) for an example. ES7 is considering embracing that pattern with some syntax sugar (http://wiki.ecmascript.org/doku.php?id=strawman:async_functions http://wiki.ecmascript.org/doku.php?id=strawman:async_functi...).
- cwmma 12y agoyes generators can help you control the flow as you do async programing, but you can't use generators to do something like an ajax function which gives you an async value, you'd need promises or something for that, generators allow you do use those async function is a more natural way.
- inglor 12y agoPromises _are_ an async value. By yielding promises you're doing exactly that :)
- aikah 12y agoGenerators dont give you async programming capabilities.You need a library that actually wraps generators into coroutines,and then wrap async functions into "thunks" to make it work.
- chadscira 12y agoThey work together beautifully, i wrote this little prototype/promise helper for node (https://github.com/icodeforlove/node-promise-object#deferred-generators https://github.com/icodeforlove/node-promise-object#deferred...). And it removes all of the boiler plate associated with using promises, and generators (if you're ok with argument stuffing).