Earlier quoted context omitted.
How is it not?
Many reasons, for example: 1. do notation works and is used for any monadic type (e.g. lists, parsers, promises, resources like database-connection-contexts, ...) while async/await works for promises only 2. async works on the function-level and only there while do-notation can be used in any place where a normal expression can be used, including being abitrarily nested. There are more differences, but that should be…
I Avoid Async/Await
81–90 of 242 posts
Re: I Avoid Async/Await
#82I believe author misses one important point. Async/await optimizes for throughput, not latency. Use case is many lightweight tasks you want to parallel as wholes. Web APIs, web sites are usual examples. However, if you need other kind of optimizations, like two parallel saves within one request, maybe async/await is not what you need in the first place.
Re: I Avoid Async/Await
#83Re: I Avoid Async/Await
#84Re: I Avoid Async/Await
#85Earlier quoted context omitted.
Many reasons, for example: 1. do notation works and is used for any monadic type (e.g. lists, parsers, promises, resources like database-connection-contexts, ...) while async/await works for promises only 2. async works on the function-level and only there while do-notation can be used in any place where a normal expression can be used, including being abitrarily nested. There are more differences, but that should be…
I don't think we disagree. JavaScript should've gone with do notation precisely because it's a generalization of async/await.
do-notation doesn't really help to deal with promises, it even makes it harder because it is confusing at the beginning. Async/await makes using promises easier and hides problems for some time at least. I can see why they chose to use async/await.
Just to give an example: error handling with try/catch. That doesn't work with do-notation, but with async/await you can integrate it (more or less at least) and it looks like sync code.
Re: I Avoid Async/Await
#86Earlier quoted context omitted.
This, very much this. async function() {} is much nicer than function() { return new Promise( (resolve) => resolve() ) }
I prefer async/await but this is a contrived example. For starters you can just write: const myfunc = () => new Promise(resolve => resolve()) or Promise.resolve()
const myfunc = () => new Promise(resolve => resolve())
Is there anyone that actually likes this structure? I find it really hard to reason about what a line like this does. What is the upper limit on double arrows in one line?Re: I Avoid Async/Await
#87Earlier quoted context omitted.
I prefer async/await but this is a contrived example. For starters you can just write: const myfunc = () => new Promise(resolve => resolve()) or Promise.resolve()
As soon as you need to chain Promises, this gets very annoying and you quickly find yourself in a nested callback hell.
Re: I Avoid Async/Await
#88Now 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.
In fact, my immediate intuition with the await examples was to parallelize with Promise.all.
await Promise.all([/* build promises */]);Re: I Avoid Async/Await
#89Earlier quoted context omitted.
I prefer async/await but this is a contrived example. For starters you can just write: const myfunc = () => new Promise(resolve => resolve()) or Promise.resolve()
const myfunc = () => new Promise(resolve => resolve()) Is there anyone that actually likes this structure? I find it really hard to reason about what a line like this does. What is the upper limit on double arrows in one line?
const wait = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms));
then: async function () {
await wait(1000);
}
Perhaps it's hard to grok but it sure is a handy one-liner.Of course in newer versions of node we can now do:
import {
setTimeout,
} from 'timers/promises';
await setTimeout(100);Re: I Avoid Async/Await
#90First off, async/await is promises. It's merely syntactic sugar. The point of async/await is not to never have the word "Promise" appear in your code. It's also not meant to be universally better than using Promises bare-bones. A lot of the argument appears to be the author extrapolating from his own lack of familiarity to others: "We are taught", "our minds", etc. I can easily construe some hypothetical person with…
Async/await is certainly not promises. In fact it would be much better implemented without promises as I proposed here: https://es.discourse.group/t/callback-based-simplified-async... I would even say that async/await is anti-promise, it takes the main functionality of promises, a caching layer for results and errors that allows you to add the code continuation later and elsewhere (which is a major footgun imo) and c…