24 Jul 2024
1 min

race – RxJS Reference

race

race is used to select an observable sequences that is the first to produce values. As soon as one of the sequences starts emitting values the other sequences are unsubscribed and completely ignored.

The resulting stream completes when the selected input stream completes and will throw an error if this one stream errors out. It will also never complete if this inner stream doesn’t complete.

The operator works in the following way:

  1. Subscribe to all source observables
  2. When a new value arrives from a source observable, pass it down to an observer
  3. Unsubscribe from all other source observables
  4. After the main source observables completes, send the complete notification to the observer.
  5. If the source observables throws an error, send the error notification to the observer.

In the diagram below you can see the race operator combining two streams A and B each producing 3 items but only the values from the stream A are emitted since this stream starts emitting values first.

race operator diagram

Usage

This operator can be useful if you have multiple resources that can provide values, for example, servers around the world, but due to network conditions the latency is not predictable and varies significantly. Using this operator you can send the same request out to multiple data sources and consume the result of the first that responds.

Here’s an example of using race to run multiple HTTP requests and cancel one of them:

const a = fromFetch('https://api.mocki.io/v1/0350b5d5');
const b = fromFetch('https://api.mocki.io/v1/ce5f60e2');

race(a, b).subscribe((response) => console.log(response));

Playground

Additional resources

See also

Frequently asked questions

How does the RxJS race operator choose which observable to use?

The race() operator subscribes to all source observables and selects the first one that emits a value. Once that happens, it unsubscribes from all the other sources and ignores them completely. The resulting stream then follows only the winning source, passing its values down and completing or erroring when that source does.

When should I use race() in RxJS to send the same request to multiple data sources?

Use race() when you have several resources that can provide the same value, such as servers in different regions, and their latency is unpredictable. You send the request to all of them and consume the result of the first one that responds. The slower requests are cancelled automatically because their subscriptions are unsubscribed once a winner emits.

Does race() complete when all source observables complete in RxJS?

No, the resulting stream completes only when the selected winning source completes. If that source never completes, the stream never completes either. Errors work the same way: an error from the selected source is propagated to the subscriber, while errors from the sources that were already dropped are no longer relevant.

What is the difference between race() and merge() in RxJS?

race() picks a single winning source, the first to emit, and ignores all others after that point. merge() subscribes to every source and emits values from all of them interleaved as they arrive. Choose race() when you want only one result from several alternatives, and merge() when you need values from every stream.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.