shajaraFlow ExplorerFirst Result
English

First Result

race(...) returns the first successful value after canceling the remaining work.

race(readCache, fetchNetwork)readCachefetchNetworkwinnerremaining workfirst resultloadProfilependingreadCachependingfetchNetworkpendingrace resultpending
function* loadProfile() {
  const profile = yield* race([
    function* readCache() {
      yield* sleep(1000);
      return "cached profile";
    },

    function* fetchNetwork() {
      yield* sleep(6000);
      return "fresh profile";
    },
  ]);

  return profile;
}
loadProfile calls race(...), starting readCache and fetchNetwork.
readCache wins; race(...) cancels fetchNetwork before returning the profile.