3 ms·
A lot of these features look great, and some of them are even useful. I think it's a shame that async/await has to wait for ES7. It would really give some direc
by rzimmerman 12y ago
A lot of these features look great, and some of them are even useful. I think it's a shame that async/await has to wait for ES7. It would really give some direction to the node community and give JS an edge for async programming. Generators just look like another awkward attempt to avoid callbacks by jumping through different hoops.
- aaron-lebo 12y agoGenerators have long been used in the Python community for reasons other than callback avoidance (they are lazy sequences). Generators in ES6 (as well as several other features) seem to be inspired by Python. There's no reason we'll have to wait for ES7. Seeing as how good 6 to 5 transpilers already are, I'm sure you'll be able to use await relatively soon.
- rzimmerman 12y agoGenerators in Python are a very useful feature (lazy sequences, like you said), so maybe I spoke to soon. However, I don't think people should be lauding this feature as some kind of solution to callback hell. I just feel like priority-wise, users would benefit most from modules (which made it), async/await, maybe class syntactic sugar, then everything else.
- masklinn 12y ago> I don't think people should be lauding this feature as some kind of solution to callback hell. Then async/await is no solution for it either.
- rzimmerman 12y agoI think: app.get('/thing', function (req, res) { try { await user = db.get({user: req.user}); res.send({user:user}); } catch { res.send(400); } }); beats the hell out of: app.get('/thing', function (req, res) { db.get({user: req.user}, function (err, user) { if (err) return res.send(400); res.send({user:user}); }); }); especially when you have conditionals or loops that can look like: if (user.name == 'abc') { resp = 5; } else { await resp = db.get_resp({user:user}); } await db.save(something); I suppose it doesn't get rid of callbacks, just makes them readable.
- nawitus 12y agoAnd the promise version for comparison: app.get('/thing', function (req, res) { db.get({user: req.user}).done(user => { res.send({user:user}); }, error => { res.send(400); }); });
- masklinn 12y ago> I suppose it doesn't get rid of callbacks, just makes them readable. You are the one who stated generators were no solution to callback hell but async/await was, yet here's what your code looks like with generators: app.get('/thing', function* (req, res) { try { let user = yield* db.get({user: req.user}); res.send({user: user}); } catch { res.send(400); } }); ...
- RobertKerans 12y agoFWIW, most of the stuff I've read on using generators to wrangle async has stressed that that's not the aim of the protocol, it's just a useful side-effect. But I think you are right, which is a bit of a shame really, given how useful generators can be - most every detailed article quickly skips through pretend toy examples, then <drumroll>aaaand async
- jsprogrammer 12y agoYou can already use await [1]. Also, Traceur got a patch in master for async generator functions earlier this week. [1] http://kangax.github.io/compat-table/es7/ http://kangax.github.io/compat-table/es7/
- tracker1 12y agoAs much as it sucks that it probably won't be in v8 or iojs native for a bit.. you can use co/koa for now, or you can use 6to5 or another transpiler to get the async/await features now.