Referencetake
Reference · 14
take
take
Takes the first N values and completes the stream.
Signature
take(count: number): MonoTypeOperatorFunction
Marble diagram
in: --1--2--3--4--|
take(2): --1--2|
What it does
Passes the first count values through, then sends complete itself and unsubscribes from the source. It's an automatic "unsubscribe by count".
When to use
When you need a "one-shot" snapshot of an infinite stream: a BehaviorSubject's first emission, the first click, a fixed number of interval ticks. take(1) is often used instead of first() when an EmptyError is unwanted.
See also
first, takeUntil, takeWhile
script.ts
CONSOLE · Console output
Hit Run to see the result...