Earlier quoted context omitted.
But in JavaScript, these two awaits will not happen in parallel, you really need to await Promise.all() for that.
You've really missed the point spectacularly of that example. 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...
I Avoid Async/Await
181–190 of 242 posts
Re: I Avoid Async/Await
#182I understand there are two sides/groups here: 1) Those who think errors are important to control flow, and 2) Those who think errors are exceptions to control flow. If you are in group 2 both Promises and async/await will give you neat and simple code. But if you are in group 1 Promises and async/await will be really complicated and ugly because each await will be inside a try/catch. Because I'm in group 1, I try to…
Also don't forget about co-routines:
co(function* () {
var user = yield getUser();
var comments = yield getComments(user);
});
Just replace "co(" with async, and yield with await, and you'll have async/await.Re: I Avoid Async/Await
#183Now maybe it’s just my familiarity with Promises, but I look at the third example and I can quickly see an opportunity. 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.
I didn’t read the article like that at all. 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.
Re: I Avoid Async/Await
#184Re: I Avoid Async/Await
#185Now maybe it’s just my familiarity with Promises, but I look at the third example and I can quickly see an opportunity. 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.
What the author wants is something like this: 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.
Re: I Avoid Async/Await
#186I never liked promises when they were first introduced because I never perceived this "callback hell" to be an unmanageable problem; there are excellent libraries to manage callback composition, e.g. https://caolan.github.io/async/v3/ When Promises came along I didn't feel they added much value; in fact it made things more complicated (new paradigm, harder to compose). Still, I went with it because everyone else went…
Re: I Avoid Async/Await
#187Earlier quoted context omitted.
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?
I think you’re trying to recreate the semantics of Promise.all without using Promise.all. 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.
Re: I Avoid Async/Await
#188Re: I Avoid Async/Await
#189Earlier quoted context omitted.
One thing I really like async/await for is you can use them in for-loops. It makes it significantly easier to do async work in what was previously very synchronous.
I thought await in loops was a big no no, unless it has changed since I last checked? Doesn’t it remove any opportunity for parallelism, making the loop take much longer than it would with a Promise.all? Or am I missing something?
Re: I Avoid Async/Await
#190Earlier quoted context omitted.
But in JavaScript, these two awaits will not happen in parallel, you really need to await Promise.all() for that.
In Javascript, a Promise is started as soon as it is created. In other words, this is not the `await` that starts the Promise. If the first await is the slowest, the second one will return immediately (like calling .then on an already resolved promise).