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?

6
  • Are you saying that you wouldn't even be able to call this method unless you tagged it as async? Commented Feb 15, 2023 at 0: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? Commented Feb 15, 2023 at 0: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. Commented Feb 15, 2023 at 0:57
  • it would return a promise and you still need to await it to get the returned boolean value. Commented Feb 15, 2023 at 1:05
  • Somewhat related stackoverflow.com/q/46900782/1318694 Commented Feb 15, 2023 at 2:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfectly answers my question, thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.