'Read values from one observable but switch to another once it (the other one) emits

My desired behaviour:

  1. Run HTTP request
  2. Immediately look up data in async cache
  3. If cache has the value before HTTP emits - use cache value.
  4. Use HTTP value after it's finally here.
  5. If HTTP responds faster than cache - ignore cache.

So basically I would like to kick off two async processes, one of which is supposed to provide a value quickly but if it doesn't - I want to only use the value from a slower observable which takes precedence anyway.



Solution 1:[1]

Maybe it is worth looking at the raceWith: https://rxjs-dev.firebaseapp.com/api/operators/raceWith

Basically it would look like:

server$.pipe(raceWith(cache$)).subscribe(/*side effect that must be done*/);

The thing missing is that it does not fulfill requirement 4.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 daflodedeing