API · Select

find()

Emit the first value accepted by a synchronous predicate, then stop this branch.

Example

const firstOverdue = await exstream(invoices)
  .find((invoice) => invoice.status === 'overdue')
  .single()

Parameters

fn

Type (value, context) => unknownRequired

Runs synchronously for successful values until its first truthy result. A TypeScript type predicate narrows the output type. Declare context only when needed.

Behavior

The matching value is included. If nothing matches, the stream ends without emitting a value. find() is equivalent to filter(fn).take(1): it preserves order, adds no independent buffer, and destroys its consuming branch immediately after the match so upstream work can be cancelled when no sibling still needs it.

Record errors pass through and do not call fn. A handled error does not end the search. Context follows the matched value.

Errors

If fn throws, the failure becomes a record error for that input. find() does not await promises. When a decision requires asynchronous work, attach its result with mapAsync() before filtering and taking the first match.

Forms

stream.find(predicate)
exstream.pipeline().find(predicate)

Signature

find<S extends T>(fn: (value: T, context: C) => value is S): Exstream<S, C>
find(fn: (value: T, context: C) => unknown): Exstream<T, C>

filter(), findWhere(), head(), stopWhen()