Skip to content

Function: iif()

iif<T>(condition, trueStream, falseStream): Stream<T>

Defined in: streams/iif.ts:16

Creates a stream that chooses between two streams based on a condition.

The condition is evaluated lazily when the stream is subscribed to. This allows for dynamic stream selection based on runtime state.

Type Parameters

T

T = any

The type of the values in the streams.

Parameters

condition

() => boolean

A function that returns a boolean to determine which stream to use. It is called when the iif stream is subscribed to.

trueStream

The stream to subscribe to if the condition is true.

Stream<T> | Promise<T> | T[]

falseStream

The stream to subscribe to if the condition is false.

Stream<T> | Promise<T> | T[]

Returns

Stream<T>

A new stream that emits values from either trueStream or falseStream based on the condition.

Examples

From iif.spec.ts:5

typescript
const condition = (value: number) => value > 5;
const trueStream = from([10, 20, 30]);
const falseStream = from([1, 2, 3]);
const pipeline = from([6]).pipe(
  switchMap((value: any) =>
    iif(() => condition(value), trueStream, falseStream)
  )
);
const result: any[] = [];
const subscription = pipeline.subscribe({
  next: (value: any) => result.push(value),
  complete: () => {
    expect(result).toEqual([10, 20, 30]);
    subscription.unsubscribe();
    done();
  },
});

Released under the MIT License.