by zazibar · 4y ago · 0 comments
This entire article is built around the author's ignorance and could easily be summarised as "I avoid async/await syntax because I'm more familiar with promises". The author doesn't even appear to understand that async/await is syntactic sugar for promises.
How would you handle two asynchronous saves which can happen in parallel without using a Promise.all? Don’t think you can…and that’s pretty much the entire point of the article.
Async/await is useless unless you are willing to serialize your calls defeating the entire point of async code.
In fact, my immediate intuition with the await examples was to parallelize with Promise.all.
await Promise.all([/* build promises */]);This question doesn't make sense. Async/await is just a nicer syntax for interacting with promises. So my answer to your "gotcha" question is just:
await Promise.all([..., ...])
There's nothing impure going on here. The majority of the time, async/await can make it much easier to see a code's control flow by getting rid of most of the Promise related cruft and callbacks.I would call Promise.all a benefit here, as it makes it stand out where I'm doing something in parallel.
const x = somethingAsync();
const y = somethingAsyncToo();
return { foo: await x, bar: await y }
There is no point in returning one before the other because you need both?You’re effectively saying that Promises are a better async programming paradigm than async/await…which is also what the author is saying in the article.
Both those promises start, and both are waited for after both have started..
That is the same as promise.all... There's just an explicit order for the wait, rather than as they resolve, but the result is the same.
Now, promise.any.... You'd have a point...
If the first await is the slowest, the second one will return immediately (like calling .then on an already resolved promise).
const fooP = fetch(a)
const bar = await fetch(b)
const foo = await fooPLearning the event loop, then promises, then async/await is a must. Today, you probably should throw typescript on top.
A steep learning curve just to get back to a typed language that can do things concurrently.
You do get used to it, but it is a mess of stuff.
What's hard is thinking about how to coordinate the work they are doing for you: when to consider them done, how to ask them if they did the work successfully, what to do if they need to use the same tool at some point during the work etc.
Even if concurrency is easier to get right on node I'd say the node ecosystem has just layered on complexity in other ways to get to something just as difficult to use overall.
Promises and async/await sugar are only the tip of the iceberg.
Makes using a bit of JavaScript relatively simple, just not much in Stack Exchange yet which means reading docs..
async {
save()
save()
} catch (Exception e) {
console.log("Handle error")
}
async does not deliver this at all.the author is conflating parallel with concurrent programming.
and in (the mono-thread world of) javascript the two calls will still occurs sequentially.
Consider that 'save()' might do multiple steps under the covers (network, database, localstorage, whatever). Allowing those steps to run interleaved, if necessary (with Promise.all), might be quite different from serializing the 'save()' calls completely in the caller.
So while it is true that neither is truly parallel in the "parallel vs concurrent" sense, it is not true that the "sequential"/"concurrent" execution of both styles has the same performance characteristics.