Closed alexey-milovidov closed 2 months ago
Zach (3 days ago)
you should use Promise.any() instead of Promise.race() I think - this should achieve the behavior you're looking for Since you're already using fetch API, you can create some abortController s to cancel any remaining requests once the Promise.any() is fulfilled (I think it may work)
Fabio Neves (3 days ago)
Promise.any gives the desired semantics. With the AbortController we can prevent running out of max requests
const fetchRequests = [];
const abortController = new AbortController();
for (let i = 0; i < N; i++) {
const fetchRequest = fetch(url, { signal: abortController.signal })
.then(response => {
if (response.ok) {
abortController.abort();
return response.json();
}
throw new Error('Request failed');
})
.catch(error => {
console.error(error);
});
fetchRequests.push(fetchRequest);
}
Promise.any(fetchRequests)
.then(data => {
console.log('First request succeeded:', data);
abortController.abort();
})
.catch(error => {
console.error('All requests failed:', error);
});
I think this is a sample that mimics what we want
Done.
Ideally, we can also cancel all pending requests on query change or on Zoom.
I use
Promise.race
to use the first reply among the self-managed and Cloud service - so we can break and experiment with the Cloud service fearlessly, but it still provides scalability benefits.But my implementation has the following flaws: