4 ms·
> I don't think most people working on Node want a Rails equivalent It wouldn't really be possible anyway. Async programming is hard and the shortcuts one can
by aikah 11y ago
> I don't think most people working on Node want a Rails equivalent
It wouldn't really be possible anyway. Async programming is hard and the shortcuts one can take with synchronous Ruby are impossible with Async Javascript. Nodejs has a "fibers" lib though , but it doesn't look like it is widely popular, but AFAIK it is the only to truly abstract async programming.
- danneu 11y agoUsing abstractions like http://koajs.com/ http://koajs.com/ and https://github.com/tj/co https://github.com/tj/co make Node code feel like every other language. var user; try { user = yield db.insertUser('foo') } catch(err) { if (err.code === '23505') { this.flash['error'] = 'Username taken'; this.redirect('/register'); return; } throw err; }
- aikah 11y agono because you can't write a library which apis are thunks. And your code needs to run in a generator, something you omitted. Fibers are a better alternative , no need for yield and generators everywhere.
- danneu 11y agoWell, I was earnestly sharing run-of-the-mill Koa code since you made async abstractions sound elusive. Didn't realize you just had an agenda. The thing is that people are actually using Koa/co to solve this problem. I've yet to see someone use fibers as their core webapp control flow abstraction.
- aikah 11y agoWhat agenda do I have ? I have none, I'm just saying using generators do not abstract async calls. The only way to do that is by using fibers. I'm not pushing for anything, i'm just saying that something like rails couldn't be written in nodejs , (and no sails , is nothing like rails in terms of complexity ).
- danneu 11y agoIt's your fiber agenda. My mother used it to try to make me eat my brussels sprouts and I thwarted that, too.
- girvo 11y agoECMAScript 7 has brought true async/await into the language, which currently works in Babel on top of Promises. As far as a regular user is concerned, define a function as "async" and then "await" on the result you're after inside of it. Hardly difficult, in my opinion!
- spankalee 11y agoasync/await is nice syntax, but it doesn't change the fundamental difference between synchronous and asynchronous code and the viral nature of async functions. Sync functions cannot properly compose async functions in general. A great essay on this is "What Color is Your Function": http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...
- ivank 11y agoTry bluebird's Promise.coroutine, I use it everywhere and it works great. https://github.com/petkaantonov/bluebird/blob/master/API.md#promisecoroutinegeneratorfunction-generatorfunction---function https://github.com/petkaantonov/bluebird/blob/master/API.md#...
- Dashron 11y agoOr if you want to use native promises, I threw this together https://github.com/Dashron/roads-coroutine https://github.com/Dashron/roads-coroutine
- TheAceOfHearts 11y agoWith async functions you interface with everything using promises, it's great.