The race
function of rxjs
library is used to race multiple observables against each other and emit the first value that is emitted by any of the observables. Here's an example of how to use it:
index.tsx218 chars9 lines
In this example, we have two observables source1$
and source2$
that emit their respective values after a delay of 1 and 2 seconds respectively. We pass these observables to the race
function which will race them against each other and emit the first value that is emitted. In this case, since source1$
emits its value first after 1 second, it will be the winner.
The subscribe
method is used to listen to emitted values. Here, we simply log the winner value to the console.
Note: of
and delay
functions are also imported from rxjs
.
gistlibby LogSnag