Produces a one-value iterable, with the first emitted value.
Without the optional predicate, it is the same as take(1).
take(1)
import {pipe, first} from 'iter-ops';const i = pipe( [10, 20, 30], first());console.log(...i); //=> 10console.log(i.first); //=> 10 Copy
import {pipe, first} from 'iter-ops';const i = pipe( [10, 20, 30], first());console.log(...i); //=> 10console.log(i.first); //=> 10
When the optional predicate is provided, the first value satisfying it will be emitted.
import {pipe, first} from 'iter-ops';const i = pipe( [1, 2, 3, 4, 5], first(a => a % 2 === 0) // first even number);console.log(...i); //=> 2 Copy
import {pipe, first} from 'iter-ops';const i = pipe( [1, 2, 3, 4, 5], first(a => a % 2 === 0) // first even number);console.log(...i); //=> 2
Note that the predicate can only return a Promise inside an asynchronous pipeline, or else the Promise will be treated as a truthy value.
Promise
Optional
Produces a one-value iterable, with the first emitted value.
Without the optional predicate, it is the same as
take(1)
.When the optional predicate is provided, the first value satisfying it will be emitted.
Note that the predicate can only return a
Promise
inside an asynchronous pipeline, or else thePromise
will be treated as a truthy value.