Race condition/out of order results possible when data.src is an async function
See original GitHub issue-
System Information
- Browser type and version Any (I’m on Chrome 94.0.4606.114)
- OS type and version Any (I’m on Chrome OS)
- WINDOWS: be sure to indicate which terminal you’re using – (i.e., cmd.exe, powershell, git- bash, cygwin, Ubuntu via windows subsystem for linux, etc…)
- Node version (IF applicable)
- Any error messages that may be in the console where you ran npm start
- Any error messages in the JS console
-
Describe the bug
I have a simple async data.src function like this in the config that gets autocomplete results from an endpoint. I’m also using a no-op search engine so I just rely on results from the endpoint.
...
src: async (query) => {
const response = await fetch(url);
let json = await response.json();
... // do processing on json
return json;
}
...
searchEngine: (q, r) => r,
...
The problem is that if the endpoint returns things out of order the final displayed results could be old and not make sense with the query. In practice this happens a lot because the geolocation API I’m using is rather unreliable (in terms of speed).
- To Reproduce
This is difficult to reproduce because it requires an autocomplete endpoint/API to return results out of order. I’m using geoapify.
- Expected behavior
Results from the src function be rejected if newer results have already been returned, or else some workaround allowing old results to be rejected.
I have tried this workaround which does work, but leaves uncaught errors:
...
src: async (query) => {
const sent_timestamp = (new Date()).getTime();
const response = await fetch(url);
let json = await response.json();
// Reject out of order results
if (sent_timestamp < newest_timestamp) {
throw Error("Old results");
}
newest_timestamp = sent_timestamp;
... // do processing on json
return json;
}
...
I’m new to promises/async so might be missing some other way to reject old results cleanly. But regardless I believe this should be handled automatically.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5

Top Related StackOverflow Question
Haha! Can’t believe I misread that in your code, silly me 😛 I didn’t even read the keyword
throw, I just read theErrorpart and wrote my comment. Good thing accidentally helped you anyway!Returning an Error instead of throwing it works just fine for me! Thanks! Also, FYI, I found jQuery autocomplete had this same issue and a fix was implemented: https://bugs.jqueryui.com/ticket/5982