method Stream.filter
Stream.filter<U extends T>(fn: (value: T) => value is U): Stream<U, E>

Similar to Array.prototype.filter, but works on the stream of results. If the provided function throws an error or returns a rejected promise, the error will be collected and emitted as an error result in the stream.

Examples

Example 1

import { Stream } from "anabranch";

const stream = Source.from<number, string>(async function* () {
  yield 1;
  yield 2;
});

const filteredStream = stream.filter(async (value) => {
  if (value === 2) {
    throw new Error("Value cannot be 2");
  }
  return value % 2 === 1;
});

Type Parameters

U extends T

Parameters

fn: (value: T) => value is U

Return Type

Stream<U, E>

See

Stream.filter(fn: (value: T) => Promisable<boolean>): Stream<T, E>

Parameters

fn: (value: T) => Promisable<boolean>

Return Type

Stream<T, E>