3 ms·
well IMO that is much more difficult to read for me. Promises are nice because they have a single purpose, to resolve or reject a promised value. So when I see
by Klathmon 7y ago
well IMO that is much more difficult to read for me.
Promises are nice because they have a single purpose, to resolve or reject a promised value. So when I see them, I can pretty much instantly understand what it's doing. `Promise.all` tells me that it's waiting for all of them to resolve at once, `Promise.race` tells me that the result will be the first of them to resolve or reject.
With callbacks, everything is "custom". So looking at your example I'd need to fully read the function to get an understanding of what it's doing, vs just knowing that Promise.all means "wait for them all and then resolve".
Granted, if where you are working tends to avoid promises or heavily uses a language like golang where your style is much more common, then I actually completely agree with you that the callback version is going to be more maintainable, readable, and easier to work with in your team.
But if/when your org/team collectively learns the "shorthand" that promises can provide, IMO they become much simpler to read and understand in most cases. It's basically a way of providing structure to really common patterns like yours, so that the actual structure fades into the background and the important stuff (what you are trying to do) becomes the only focus.
In other words it's syntactic sugar. With all the benefits and drawbacks that any syntactic sugar provides.
- gbuk2013 7y agoI agree with you now that "async/await" is available (as I said in my article that I originally linked to). And thank you for providing a second use case to the one I listed in the article - it paid for all the time I wasted on HN today with this thread. ;) The little bit of magic that makes your code sane is the "await" in front of "Promise.all()" and makes it more elegant than the callback version.
- Klathmon 7y ago>The little bit of magic that makes your code sane is the "await" in front of "Promise.all()" and makes it more elegant than the callback version. I'd actually say that it's the `Promise.all` that makes it sane, the await could be replaced by a `.then` and be functionally the same. Actually it might be even easier to read to someone who doesn't live and breathe javascript as it will keep the program flowing top-to-bottom: Promise.all([a(), b(), c()]).then(([a, b, c]) => { // do stuff... }) As with most things javascript, there are 9 different ways of doing it, and 3 of them will shoot you in the face...
- gbuk2013 7y agoWow that is ... beautiful. :)) And if the individual functions have the custom error logic inside then a simple ".error(callback)" at the end it will just work.