Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Run independent async calls concurrently with Promise.all instead of awaiting them one after another
When two or more async operations don't depend on each other's results, issue them concurrently with Promise.all rather than awaiting them sequentially — sequential awaits needlessly add their latencies together instead of overlapping them.
Bad example
| 1 | const author = await getAuthor(id); |
| 2 | if (!author) throw new NotFoundError(); |
| 3 | const feed = await getFeed({ authorIds: [id] }); |
Explanation (EN)
getFeed doesn't need the result of getAuthor, but it only starts after getAuthor finishes, so total latency is the sum of both calls instead of the max.
Objašnjenje (HR)
getFeed ne treba rezultat getAuthor poziva, ali počinje tek nakon što getAuthor završi, pa je ukupna latencija zbroj oba poziva umjesto maksimuma.
Good example
| 1 | const [author, feed] = await Promise.all([getAuthor(id), getFeed({ authorIds: [id] })]); |
| 2 | if (!author) throw new NotFoundError(); |
Explanation (EN)
Both calls run concurrently, so total latency is roughly the slower of the two rather than their sum.
Objašnjenje (HR)
Oba poziva izvršavaju se istovremeno, pa je ukupna latencija otprilike jednaka sporijem od njih dvoje, a ne njihovom zbroju.