by aaron-lebo · 11y ago · 0 comments
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.
I just feel like priority-wise, users would benefit most from modules (which made it), async/await, maybe class syntactic sugar, then everything else.
Then async/await is no solution for it either.
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.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);
}
});
... app.get('/thing', function (req, res) {
db.get({user: req.user}).done(user => {
res.send({user:user});
}, error => {
res.send(400);
});
});Also, Traceur got a patch in master for async generator functions earlier this week.