Skip to content

Function: some()

some<T>(predicate): Operator<T, boolean>

Defined in: operators/some.ts:21

Creates a stream operator that tests if at least one value from the source stream satisfies a predicate.

This operator consumes the source stream and applies the provided predicate function to each value.

  • If the predicate returns a truthy value for any element, the operator immediately emits true and then completes, effectively "short-circuiting" the evaluation.
  • If the source stream completes without the predicate ever returning a truthy value, the operator emits false.

This is a "pull-based" equivalent of Array.prototype.some and is useful for validating data streams. The operator will emit only a single boolean value before it completes.

Type Parameters

T

T = any

The type of the values in the source stream.

Parameters

predicate

(value, index) => CallbackReturnType<boolean>

The function to test each value. It receives the value and its index. It can be synchronous or asynchronous.

Returns

Operator<T, boolean>

An Operator instance that can be used in a stream's pipe method.

Examples

From some.spec.ts:12

typescript
let subject: ReturnType<typeof createSubject<number>> = createSubject<number>();

let source: Stream<number> = subject;

const predicate = (value: number) => value > 2;
const someStream = source.pipe(some(predicate));
const results: boolean[] = [];
(async () => {
  for await (const value of eachValueFrom(someStream)) {
    results.push(value);
  }
})();
subject.next(1);
subject.next(2);
subject.next(3);
subject.complete();
await new Promise((resolve) => setTimeout(resolve, 100));
expect(results).toEqual([true]);

Released under the MIT License.