Mr. Tono types in the search box and a runner leaves for every letter: whose answer ends up on the board? Add a request number, an AbortController or a…
Why it matters
Search results that "jump back", the wrong product details after quickly switching pages, a form showing old data: all race conditions. Tests on a fast network never catch them, because there the answers happen to arrive in order.
The key idea
await waits for its own answer, not for the order between requests
Common mistakes
Using await and thinking the order comes with itawait only orders things within one function. Between requests, whoever arrives last wins. Mark the newest request or abort the old one.
Only checking q === input.value on resolve, still mixed up when the user types back to the same text The same word can have several requests. Compare the requests (a sequence number or an AbortController), not the text.
A sequence-id guard in place, but the server is still flooded A guard only drops the answer; the request is still sent. Use a debounce (clearTimeout + setTimeout) so a request goes only once the user stops typing, and an AbortController for the ones already on their way.
Try it in this playground
Watch an old answer overwrite the new one Run 'sat', then 'sate' until answer #1 arrives.
An order number drops the old answer Pick the id === terakhir guard.
AbortController calls the runner back Pick the AbortController guard.
The same text, a stale answer Open Typing back with the q === kotak.value guard.