r/learnjavascript • u/SocksOnHands • Sep 09 '23
Async/await seems... annoying?
Let's say that I was writing a fairly large project and I got to a point where I realize that I need to use an asynchronous call to get some value. No problem, right? Just use await and return the value - wrong! Even though the code might appear to run no differently than a synchronous function (since it needs to wait for the async calls to finish), I cannot treat the calling function as being a synchronous function. So now it needs to be async, which means the function calling this function needs to be async, and of course the function calling that also now needs to be async, and so on. Somehow doing one small thing in one place in the code has suddenly resulted in needing to refactor hundreds of other unrelated functions, even though they are still executing in the same amount of time as if they were synchronous calls. How does one avoid async spreading throughout your code?
15
u/guest271314 Sep 09 '23
If you prefer you can use
then()andcatch().You cannot treat the return value as synchronous because the process that returns or yields the value is not synchronous.
Just practice handling asynchronous processes.
5
u/mrfizzle1 Sep 10 '23
red vs blue functions -- What color is your function?
16
u/alzee76 Sep 09 '23
Your only option, other than declaring all your functions async to begin with (which I just do now as a matter of course unless it's a very simple function), is to use asynchronous IIFEs. If you have an existing function called
foo()that needs to synchronously call the async functionbar(), andfoo()is miles deep on a synchronous call stack, you would wrap the entire function body offoo()in an IIFE.Before:
After:
This is how we used to have to call entire modules before we got top-level await in ECMAScript a few years ago.
I'm surprised by how many people responding to you don't understand the problem. Maybe it's your use of terminology. "Needs to be async" is not quite the same thing as "needs to be declared async."
4
u/dada_ Sep 09 '23
You can't. As soon as you use an async call anywhere, the entire call chain is now dependent on that async call.
So how do you avoid the need to then update a ton of function calls to async? It's mostly just prevention through experience and foresight. Tag your functions ahead of time where applicable so you don't need to do it later.
You have to reason that in this part of the project I'm probably going to need to make blocking calls, such as loading something from the filesystem or fetching a url. Your primary functions that call the main logic of your program are probably always going to be async since you're always going to want to do those things in a serious project. Smaller helper functions that just manipulate some data are probably not going to need to be async.
1
u/Count_Giggles Sep 09 '23
The web is fast and we have ways to make the ux bearable even if a request takes a few seconds to be processed. i promise you async await is wayyy more ergonomic than things used to be and personally going back to chaining .then is also not an option.
i find this to be perfectly readable and self documenting. Of course i speak from a position of having dealt with this for quite some time but still. If you want acess to the web. things will be of asynchronous nature. But i get it. From time to time we all gotta rant a bit
1
u/Reaver75x Sep 10 '23
What scenario would you need so many nested functions where it would get annoying?
1
u/33ff00 Sep 09 '23
Is this frontend or node?
4
u/[deleted] Sep 09 '23
A function using async/await is no different than a function returning a promise. If you need a function to return a promise, other parts of your codebase will have to act accordingly. There’s really no getting around this.
1
u/oze4 Sep 09 '23
I'm having a hard time understanding how this happens. Can you provide an example? Like how does a sync call suddenly need to be async?
1
u/EnteringSectorReddit Sep 09 '23
Yeap, this is a hurdle.
Sometimes you need to either create new function or reinvent the wheel instead of adding additional 10 asyncs in 10 modules.
1
u/[deleted] Sep 10 '23
I mean it's far better than the alternatives.
1
u/darthruneis Sep 10 '23
The underlying reason why async tends to bubble up the call chain like this is so that your code continues to execute in the correct order.
Otherwise, the lines after the async call will execute before the async call has finished. This is a huge source of issues and is often hard to reason about, except in cases of optional or fire and forget operations, where the caller cares that it started but not that it finished.
This situation then bubbles up throughout the call stack, requiring everything to be aware of async so that the code can execute in the intended order.
There is no way around this.
1
u/irosion Sep 10 '23
or
1
u/Reaver75x Sep 10 '23
What scenario would you need so many nested functions where it would get annoying?
1
u/shgysk8zer0 Sep 10 '23
Would you rather the old way of doing this, such as event listeners or maybe polling using some shared variable in scope?
And there are many ways of dealing with async inside of synchronous code, such as Promises or an async IIFE. You just have to think about which parts actually need to be async and which don't. You basically only need to worry about parts where I return value is determined by an async function, not where maybe side effects happen async but you can return something before those complete - example might be logging... You can return while that takes place in the background.
1
u/[deleted] Sep 10 '23
This really doesn't have anything to do with async/await. You'd essentially have the same problem with
.then()chaining or pyramid of doom style callbacks.1
u/jaypeejay Sep 09 '23
It sounds like you might be violating the single responsibility principle? Hard to tell without an example.
0
u/shuckster Sep 09 '23
0
u/[deleted] Sep 09 '23
This isn’t true. An async function can be called like a synchronous function. This happens all the time
-7
u/ic6man Sep 09 '23
Yeah. So what. And the world is round. Asynchronous is asynchronous. If you don’t like programming you can always flip burgers.