This rule raises an issue when an async function does not use any asynchronous feature.
Declaring a function async signals that it suspends on await or for await, so other work can proceed while
an operation such as I/O completes.
If the function’s body never suspends, the async keyword is superfluous. Using async unnecessarily can:
async function always returns a Promise, even when its body runs
entirely synchronously. Wrapping a synchronous result in a Promise costs at least one microtask tick that a plain synchronous return
would not.await the result or chain .then() to read it, ceremony that is
unnecessary when the function was never truly asynchronous.Before changing the function, verify whether it must wait for an asynchronous operation and how callers use its return value.
await to that expression.async.
async function getUserName(user) { // Noncompliant: nothing is awaited, "async" only wraps the result in a Promise
return user.name;
}
function getUserName(user) {
return user.name;
}
async function saveUser(user) { // Noncompliant: the request is sent but never awaited
fetch('/api/users', { method: 'POST', body: JSON.stringify(user) });
console.log('User saved');
}
async function saveUser(user) { // Compliant: waits for the request before logging
await fetch('/api/users', { method: 'POST', body: JSON.stringify(user) });
console.log('User saved');
}
This rule does not raise an issue in the following cases:
async function noop() {}.return statement or a concise arrow body, such as async () ⇒
somePromise, that resolves to an already-thenable value counts as equivalent to awaiting it, and so does a for await loop.async function* that yields a thenable value, or that
delegates via yield* to an async-iterable source, counts as using an async feature even without an explicit await.Promise return annotation for this exception; for example, it can
still raise an issue on async function save(): Promise<void> if the function body does not return a Promise in any
of its branches. This covers callbacks passed as arguments or JSX attributes, functions assigned to typed variables, properties, or parameters, and
instance methods that implement or override a Promise-returning member of an interface, base class, or typed object. Callbacks passed to generic
functions or generic constructors may not provide a contract this rule can resolve.'use server' directive, or that are exported from a module
starting with 'use server'. React Server Functions must be async, whether or not they await anything.
async function stub() {} // Compliant: empty body
async function relay(id) {
return fetchUser(id); // Compliant: the return value is already a Promise
}
async function* stream(source) {
yield* source; // Compliant: delegates to an async-iterable source
}
async function refreshProfile() {
'use server'; // Compliant: Server Functions must be async
revalidatePath('/profile');
}
function load(loader: () => Promise<User | null> = async () => null) { // Compliant: the parameter type requires a Promise
// ...
}
interface Repository {
save(entity: Entity): Promise<void>;
}
class InMemoryRepository implements Repository {
private readonly entities: Entity[] = [];
async save(entity: Entity): Promise<void> { // Compliant: implements a Promise-returning interface method
this.entities.push(entity);
}
}
function SaveButton({ onSave }: { onSave: () => Promise<void> }) {
// ...
}
<SaveButton onSave={async () => { logger.info('saved'); }} />; // Compliant: the prop type requires a Promise