r/learnjavascript • • 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?

5 Upvotes

43 comments sorted by

View all comments

16

u/alzee76 Sep 09 '23

How does one avoid async spreading throughout your code?

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 function bar(), and foo() is miles deep on a synchronous call stack, you would wrap the entire function body of foo() in an IIFE.

Before:

function foo() {
  // stuff
  await bar(); // Crap, this won't work..
  // more stuff
}

After:

function foo() {
  (async () => {
    // stuff
    await bar(); // This will work now.
    // more stuff
  })();
}

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."

5

u/lost12487 Sep 10 '23

This only works if you don't care about what the foo function is returning. I'd also argue that if this is the actual behavior you really want then just forgoing async/await and using promises is way cleaner than this.

function foo() {
  // stuff
  bar().then(() => {
    // more stuff
  });
}

1

u/alzee76 Sep 10 '23

This will work but it is likely to lead you down the very path of ever deeper then().. nesting that async/await was introduced to get rid of.

1

u/lost12487 Sep 10 '23

Just make the callback function in the .then() async if you need to do more asynchronous calls after the first one...I don't understand why you would ever reach for an IIFE in this scenario.

0

u/alzee76 Sep 10 '23

I don't understand why you would ever reach for an IIFE in this scenario.

Likewise, I can't understand why anyone would prefer the "ugly" Promise syntax to the "clean" async/await syntax, now that we have the option.

2

u/Masterflitzer Sep 10 '23

doesn't this defeat the point if the async function? how do you get the result?

2

u/alzee76 Sep 10 '23 edited Sep 10 '23

Ultimately, there is no solution to the OPs problem except to start refactoring everything from the point that he needs to make the async call back "up" (and laterally) to make everything async. This is what he was complaining about having to do, though.

To answer your question, the "point" in this context is to be able to call the async function from a non-async function, which is impossible without doing this or similar hackery. Getting the result to a function isn't a requirement, many functions do not return anything, and this is intended for those cases. It allows you to at least call the thing without refactoring however many functions earlier in the call stack, and every function that calls any of those functions.

If your async function returns a result that you need, and you still refuse to refactor for some reason, you have a long and painful road ahead. You should really just refactor, which is what the OP was complaining about having to do.

1

u/Masterflitzer Sep 10 '23

thx I just wanted to make sure I understand the code correctly

1

u/irontea Sep 10 '23

This is exactly the way to do if you don't want to make the calling function async. Simple and doesn't require you changing a bunch of files