Async Function Without Own Await
ID |
javascript.require_await |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
async, code-style |
Description
Reports async functions whose body contains no await expression that belongs to that function. The async keyword wraps the return value in a Promise and forces a microtask hop on every call; when nothing inside the function actually awaits, that overhead has no purpose.
The check is scoped per function: an await inside a nested function (regular, generator or arrow) does not satisfy the outer one, since each function has its own async scope.
Rationale
// Bad — `async` adds an extra microtask, but nothing is awaited.
async function compute() {
return 42;
}
async function fetchAndReturn() {
return Promise.resolve(1);
}
The two functions above could be plain functions returning the value (or Promise.resolve(…) if the contract explicitly requires a Promise). Marking them async obscures the synchronous nature of the body and pays for a microtask on every call.
Remediation
Drop the async marker, or introduce the await the function was supposed to perform.
// Good — `async` only when the body actually awaits.
async function fetchUser(id) {
const res = await fetch(`/users/${id}`);
return res.json();
}
// Good — synchronous body, plain function.
function compute() {
return 42;
}
// Good — explicit promise wrapper makes intent obvious.
function asyncCompute() {
return Promise.resolve(42);
}