3 ms·
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
by firasd 10y ago
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... */
- spion 10y agoIf 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])
- tomc1985 10y agoIt's not so much because of promises, its because sync IO blocks the GUI thread, so you have to write callbacks somehow. Pick your poison.