Re:After a year of using Node.js in production
by woah on Monday May 02, 2016 @06:39PM
(#11616319)
Callbacks are ugly, but are probably the semantically simplest way to handle asynchronicity. Promises are ugly too, but are semantically the same thing as async/await. I agree that promises and callbacks are not pleasing to the eye, but they are completely logical ways to do things.
[ Parent
| on HN ]
Re:After a year of using Node.js in production
by firasd on Monday May 02, 2016 @06:46PM
(#11616367)
So in PHP, you would go (I haven't tested these snippets, just writing them out here):
$username = get_username();
echo "Hi, ".$username;
do_other_things();
In Node, using promises, you have to write: get_username().then(function(username) {
return res.send("Hi "+username");
}).then(function() {
do_other_things();
})
And if you're using regular callbacks, forget it: you'd have to nest do_other_things in the callback of get_username(!). It just makes things very awkward.I understand what you mean about sync vs async calls. I won't pretend I have a better solution. I don't mean to say callbacks are illogical, just a bad way to write programs. So maybe not 'wrong in concept', I can concede that. But I think async/await can make things more readable again, i.e.:
var username = await get_username();
/* carry on... */If otherThings is unrelated, you could also do this:
var username = getUsername();
var resultSent = username.then(username => res.send("Hi "+username"))
var otherThings = doOtherThings()
return Promise.all([resultSent, otherThings])