0

For our codebase, we have quite a few asynchronous calls which require the async await flow since we make a lot of API calls. However, in some of these classes, we use some functions which, in nature, do not require any async operations like if-else statements on a String, an example is given below.

static async isValidLength(name){
    if (name.length() > 5){
        return true;
    } else {
        return false;
    }
}

I want to argue since we mostly use async operations, wrapping all functions in an async await flow is more maintainable and less error-prone to forget using await in some truly async operations, which can cause the programme to behave in unexpected ways.

I tried to search the web whether there were some recommended conventions around this but I couldn't really find anything, so I'm asking for experiences/opinions on the matter here. Is it a bad practice to wrap non-async functions with the async keyword?

  • Are you saying that you wouldn't even be able to call this method unless you tagged it as `async`? – matt Feb 15 '23 at 00:45
  • No, it would completely work. My question is more targeted towards, are there bad use cases of tagging methods in async while no asynchronous operations are performed inside of said method? What are the implications of mixing `async` tagged operations with different operations not tagged as `async`? – ConfusedStudent Feb 15 '23 at 00:54
  • 1
    It an equally bad practice as `const numberThree = parseInt((3).toString())` instead of `const numberThree = 3`: unnecessary operations are unnecessary, slow down the code, and possibly impede reasoning about code. If you are worried about mixing up which functions are asynchronous, name them with `Async` suffix or something; making all functions asynchronous and throwing `await` everywhere is going way overboard. That said, you are explicitly asking for opinions, so this is outside Stack Overflow scope. – Amadan Feb 15 '23 at 00:57
  • it would return a promise and you still need to await it to get the returned boolean value. – Ahmed Eid Feb 15 '23 at 01:05
  • Somewhat related https://stackoverflow.com/q/46900782/1318694 – Matt Feb 15 '23 at 02:05
  • "are there bad use cases of tagging methods in async while no asynchronous operations are performed inside of said method?" Yes. All of them. – matt Feb 15 '23 at 02:08

1 Answers1

3

This would make your code a mess. Imagine if you wanted to reverse each word in a string, and the split(), reverse(), map(), and join() functions were all async. It would look something like this:

const reversedWords = await (await Promise.all((await str.split(' ')).map(w => w.reverse())).reverse()).join(' '));

I could stare at that for 15 minutes and still not know if I did it right (probably didn't).

Every function call will have the overhead of wrapping the result in a promise.

And you'll make the problem you're trying to solve even worse. Developers will have to put await on every single line of code they write. If they forget that anywhere: bug.

Consider using ESLint rules instead. Or simply test the code.

Aurast
  • 3,189
  • 15
  • 24