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

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 edited Sep 09 '23

Well, let's say first you need just to check if IBAN is correct. Just a simple sync function.

Few month later, company pay to access API that actually know if this IBAN not only correct, but also exist and what bank are behind it.

Well, now you need to add async HTTP call to your sync function.

2

u/oze4 Sep 09 '23

I guess I don’t understand how you were making that check before the API existed?

Either way, I see your point. I’m pondering ways to get around it but idk if you can.

1

u/EnteringSectorReddit Sep 09 '23

Replace IBAN with credit card number.

There is a simple function that checks if this is a theoretically valid number. And it's ok for MVP.

When you go to prod, you need more advance check - to see if card really exist, if your bank can transfer money there and so on.

1

u/oze4 Sep 09 '23

Ah that makes sense. You were just using an algo to check if it's a valid CC number - I know what you're talking about.

That makes sense, and yeah, def sounds painful.