We'll be building a simulation for a global application that needs to fetch username for a user as quickly as possible. To minimize latency,
there should be several replica APIs set in multiple regions.
Create three methods fetchUserUS()
, fetchUserEU()
, and fetchUserAsia()
. Each method should
return a promise which resolves with the username oliver after a delay of 200ms, 300ms and 150ms respectively. These functions simulate fetching the same username from different regional servers.
Additionally, to simulate occasional network issues, add a 20% chance of failure for
each of the regional servers.
Next, implement a method fetchFastest(fetchFunctions)
which
calls all the methods in fetchFunctions
and returns the username returned by the first successfully resolved promise.
If none of the promises returned by the methods in the fetchFunctions
get resolved, fetchFastest
should reject with the message
All requests failed.
For example: