Some developers get annoyed by async functions that don't use await functionality. There are even linter rules that don't allow the usage of async functions if they don't contain the await keyword. I'm curious to know why is this a problem?
For example, I use async functions without the await functionality in some occasions:
-
to indicate that execution will be asynchronous if that's not obvious
async function run() { return doSomething() }
-
to mock an async call or to simply prepare the code to work out of the box with asynchronous logic if transfer to asynchronous execution is anticipated. Looks cleaner than wrapping the return value with a Promise manually.
async function fetchData() { return {foo: 'bar'} }
Linter rule that denies the usage of async function without the await forces you to change the beginning of your function definition every time you decide to ditch the await and return something immediately, change the inside logic to handle multiple call in parallel or similar. For every such change you are forced to go back and forth with your function definition and create unnecessary version control mess. That's what's really annoying, at least to me(#opinion).
Thanks for the article Luka, I agree with your examples. Actually, I have a rule on my IDE that warms me of unnecessary await in the return statement.
What's your opinion about this other snippet that I see utterly pointless? Why would you add the async keyword in such a case?