Skip to main content

raceWithNotifications

Alternative: raceN

Types

function raceWithNotifications<GObservables extends IGenericRaceInObservables>(
observables: GObservables,
): IObservable<IRaceObservableNotifications<GObservables>>

Definition

Waits for the first observable to send a complete or error Notification.

If it received a complete Notification, it will emit the last value received through the next Notifications of this observable, followed by a complete Notification.

If it received an error Notification, it will immediately send this Notification (error).

This is equivalent of Promise.race.

Diagrams

If one completes

OUTraceWithNotifications([observablesA, observablesB])observablesBobservablesAOUTraceWithNotifications([observablesA, observablesB])observablesBobservablesAnext: b1next: a1completenext: a1complete

If one errored

OUTraceWithNotifications([observablesA, observablesB])observablesBobservablesAOUTraceWithNotifications([observablesA, observablesB])observablesBobservablesAnext: b1error: errorAerror: errorA

Example

Select the first Observable to complete

const observable1$ = switchMap$$(timeout(500), () => singleN<'a1'>('a1'));
const observable2$ = switchMap$$(timeout(1000), () => singleN<'a2'>('a2'));

const subscribe = raceWithNotifications([observable1$, observable2$]);

subscribe((value: IDefaultNotificationsUnion<'a1' | 'a2'>) => {
console.log(value);
});

Output:

// t = 500ms
'next', 'a1'
'complete', undefined