Awaits that could run together
Awaits that could run together. This await doesn't use the result of the one before it, so they could run at the same time.
This is how the comment reads inline, under the flagged line.
Why it matters
Each await waits for the one before. When the second call doesn't need the first one's result, the latency adds up for nothing.
What to do
Start both, then await both: await Promise.all([a(), b()]) or asyncio.gather(a(), b()).
Examples
const user = await getUser(id); const orders = await getOrders(id);
const [user, orders] = await Promise.all([getUser(id), getOrders(id)]);
What it catches
It reads the shape of the code: functions, blocks, loops and calls. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
async function f(id) {
const user = await getUser(id);
const orders = await getOrders(id);
return [user, orders];
}
src/app.py
async def f(i):
a = await get_a(i)
b = await get_b(i)
return a, b
Left alone
async function f(id) {
const user = await getUser(id);
const orders = await getOrders(user.id);
return orders;
}
async function f() {
await stepOne();
await stepTwo();
}
Research
Independent awaits run one after another. Latency adds up when the second call does not need the first one's result.
- owner's notes
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"sequential-awaits"
],
"severity": {
"sequential-awaits": "low"
}
}
}