I need to get the result of a Javascript Promise that returns the fastest, but I want to continue invoking the logic encapsulated within the other 2 "losing" promises, irrespective of who wins. Example below.
// The 3 promises I care about
const fetchFromGoogle: Promise<T> = googlePromise()
const fetchFromAmazon: Promise<T> = amazonPromise()
const fetchFromCloudflare: Promise<T> = cloudflarePromise()
// The promise that invoked its logic the fastest
const winner: T = Promise.race([fetchFromGoogle, fetchFromAmazon, fetchFromCloudflare])
In this scenario, if fetchFromAmazon call wins in terms of speed, then I would return the result to the client, but continue running the other two promises async.
This is being executed from within a Cloudflare Worker and the ability to return the winning promise whilst continuing the evaluation of the other functions will be supported via the waitUntil API linked below.
I've evaluated two options:
- Some Javascript API that I'm unaware of that can do this for me
- Use something like this to determine which promises lost and run them using
Cloudflare Workerscontext.waitUntil call which will ensure that the logic will continue evaluating despite having returned the result back to the client.
It's in my understanding Promise.All would not satisfy this criteria because I would never early return the winning promise as we wait for all 3 to complete.