shajaraExplorerFirst Result
English

First Result

race first-result selection

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";
    },
  ] as const);

  return profile;
}
loadProfile starts readCache and fetchNetwork as competing work.
race returns the readCache result while canceling fetchNetwork.