race

signature: race(): Observable

The observable to emit first is used.

Examples

Example 1: race with 4 observables

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { mapTo } from 'rxjs/operators';
import { interval } from 'rxjs/observable/interval';
import { race } from 'rxjs/observable/race';

//take the first observable to emit
const example = race(
  //emit every 1.5s
  interval(1500),
  //emit every 1s
  interval(1000).pipe(mapTo('1s won!')),
  //emit every 2s
  interval(2000),
  //emit every 2.5s
  interval(2500)
);
//output: "1s won!"..."1s won!"...etc
const subscribe = example.subscribe(val => console.log(val));

Example 2: race with an error

( StackBlitz | jsFiddle )

Additional Resources

  • race

    :newspaper: - Official docs

:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/race.ts

Last updated

Was this helpful?