Referencefirst
Reference · 20
first
first
Emits the first value (optionally matching a predicate) and completes.
Signature
first(predicate?: (v: T, i: number) => boolean, defaultValue?: T): OperatorFunction
Marble diagram
in: --1--2--3--|
first(>1): -----2|
What it does
With no arguments it's the same as take(1). With a predicate it takes the first value that satisfies the condition. If the stream completes without a match, it throws EmptyError (or emits the default, if one is given).
When to use
Get the first click, wait for an element to appear in the stream, get the first successful response. When you want the strict "otherwise error" semantics; otherwise take(1) is preferable.
Gotcha
first(predicate) and filter(predicate).pipe(take(1)) differ: the former throws EmptyError, the latter just completes quietly.
See also
take, last, find
script.ts
CONSOLE · Console output
Hit Run to see the result...