Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: no-unnecessary-async #2487

Closed
cjoecker opened this issue Oct 21, 2024 · 6 comments
Closed

Rule proposal: no-unnecessary-async #2487

cjoecker opened this issue Oct 21, 2024 · 6 comments

Comments

@cjoecker
Copy link

cjoecker commented Oct 21, 2024

Description

The rule would be beneficial in avoiding async functions that are, in reality, synchronous, and no async is necessary.
Unnecessary async function breaks unit tests that use fake timers.

Fail

async function getLast(arr: number[]) {
	return arr.at(-1);
}

Pass

function getLast(arr: number[]) {
	return arr.at(-1);
}
async function getLast() {
	return await getLastFromDb();
}

Proposed rule name

no-unnecessary-async

Additional Info

No response

@sindresorhus
Copy link
Owner

I think it's a good idea in general, but without types, it's really infeasible, and we don't have types available here. It may more appropriate for https://github.com/typescript-eslint/typescript-eslint

@cjoecker
Copy link
Author

@sindresorhus isn't it possible for the rule to check if there is an await or new Promise inside the function? If there is none, we can know there is no promise used inside the async function and show the error.

@sindresorhus
Copy link
Owner

That would miss functions that return a promise:

async function getLast(arr: number[]) {
	return thisReturnsAPromise();
}

@fregante
Copy link
Collaborator

Pretty sure this exists and it's disabled in XO.

@fregante fregante closed this as not planned Won't fix, can't repro, duplicate, stale Oct 22, 2024
@cjoecker
Copy link
Author

@fregante you are right! Those rules do that, thanks for the hint!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants