by mrozbarry · 2y ago · 0 comments
I also don't like how async/await ends up taking over a whole codebase. Often making one function async will cause you to update other functions to be async so you can do proper awaiting. We literally exchanged callback hell to async hell, and said it was better, but I'm not strictly convinced of this.
Callbacks still have the function coloring problem: If you write a function which calls another function with an on-complete callback argument, then your function (usually) has to also take an on-complete callback in order to represent completion.
async/await makes this nicer, but it doesn't eliminate the underlying need to thread async completion all the way up your call stack.
This is basically the function coloring problem. There was some ardent discussion about this a few weeks ago on HN [1], where the top comment positioned function coloring as a feature, because it highlights where expensive operations like IO might potentially be happening in your application.
I saw a comment recently where someone pointed out an old API in Java where comparing two URLs for equality initiates a DNS request, and so something as simple as putting a URL in a hash table could end up involving a network request [2]. So maybe it's reasonable to color functions that connect to the network at the very least. If I used a URL comparison library and it returned a promise it would immediately set off alarms in my mind.
It's possible, but I can't thoroughly evaluate coeffects until they land in a mainstream language with production usage. What I do know from experience is that you can't quietly block the entire thread on a network request in some innocuous looking function in JS. You have to return a promise and then the calling function has the option to await that promise, but I'm sure the academics in their ivory towers have theorized about something better.
It's obviously better. It's a bit unfortunate how much the history of the design decision shines through in the current solution, but it's unequivocally an improvement.
I would prefer an approach where calls to "async" functions are implicitly awaited unless a keyword turns then into a promise, and all functions are implictly treated as async as needed, unless a keyword specifies that they return a promise, which should be awaited instead. This would make the majority case clearer, and force you to make the minority case explicit where it's currently implicit.
I don't think this would help your coworkers who don't understand promises very much though.
> I would prefer an approach where calls to "async" functions are implicitly awaited unless a keyword turns then into a promise, and all functions are implictly treated as async as needed, unless a keyword specifies that they return a promise, which should be awaited instead. This would make the majority case clearer, and force you to make the minority case explicit where it's currently implicit.
This is a very interesting idea and feels good in an initial 'gut check' sense.
Both have their pros and cons. I've seen problems in Go codebases where some I/O operation blocks the main thread because it's not obvious through the stack that something _should_ best be run concurrently and it's easy to ignore until it gets worse (at which point it's annoying to debug).
const myAsyncThing = async (init) => {
let value = await task1(init);
value = await task2(value);
return value;
}
const myPromiseThing = (init) => task1(init).then(task2)
I know this is a relatively simple and contrived example, but there are some times where async/await very much is bulky and gets in the way of just writing code.Compare with
const myPromise = new Promise((resolve, reject) => resolve(”foo”));
Afterwards either try-catch await myPromise, or use myPromise.then().catch() const myAsyncThing = async (init) => await task2(await task1(init))
This makes the promise chain look much more like what it is, given that the rest of the language uses nesting calls to indicate "after".I treat await as a code smell and always triggers me to evaluate if we really need to be waiting for the result in this place. Most of the time they shoukd just return the promise or put something in a then.
People are going to misuse stuff but it doesn’t mean we shouldn’t use appropriate tools when appropriate.
EDIT: this is especially true for NodeJS where lots of frameworks are promise/async aware and support you returning a promise to them.
The number of await calls in your codebase should always be greater than the number of async modifiers.
Instead of using await you can instead just use “.then(() => part that needs to wait)”, or a chain of “.then” on the returned promise to sequence dependent logic. Or if needing to wait on a set of async actions you have “Promise.all”. It is much more explicit on what part actually needs to wait instead of “everything after this in the function block”.
I prefer to just compose promises which allows you to sequence actions that need it. It is a code style preference but I find I end up with clearer codebases doing it this way.
Yes. Without using await, it's basically the same thing as just changing `return result` to `return Promise.resolve(result)`.
> Useful for wrapping logic that otherwise would be synchronous.
The logic is still synchronous, except now the return value is just wrapped in a promise.
> Instead of using await you can instead just use “.then(() => part that needs to wait)”
I don't understand why this would ever be desirable versus just calling the function synchronously. If for some reason you really need `.then` I would just wrap the call to the function, instead of making the fn async, `Promise.resolve(myFn()).then(() => part that needs to wait)`
> if needing to wait on a set of async actions you have “Promise.all”.
You can pass non-promise values to `Promise.all`, and it will work fine, although I don't see why you would do it intentionally. ie `await Promise.all([1, Promise.resolve(2)])// resolves to [1,2]`.
> It is much more explicit on what part actually needs to wait
If anything it's less explicit, you are disguising the true behavior. You making a synchronous function look like it's async when it isn't.
It's also going to suffer a small performance penalty, although it shouldn't really matter unless the function is used in some tight loop. When I tested calling a very simple `return 1` function, making it async resulted in the benchmark taking twice as long.
This is actually how rust handles async/await. If nothing is polling the future then the future is not running. If you want background execution like javascript then you need to hand it off to an executor (for example via `tokio::spawn`). Its actually pretty nice because then you can build a future and pass it around but you can decide if/when you want to execute it later.
That being said, you still wouldn't want to do a for loop awaiting each future.
I have a degree in Electrical Engineering, and don't understand how programmers can function not understanding physical computer architecture.
But clearly, that's not the bar which most come from.
It's a basic way to learn. In Python you don't directly teach people about decorators without first asking them to explicitly reassign a function to the result of calling the decorator. In Haskell you don't teach people about do notation without first making them write nomadic code with the bind operator and many lambdas.
In many cases, just a few days of forbidding students to use the syntactic sugar is enough to make them appreciate why this syntactic sugar is needed and how it is supposed to be used. JavaScript's async/await is the same.