3 ms·
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
by woah 11y ago
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.
- firasd 11y agoSo 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 11y 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 11y 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.