take passes values from the source observable to the observer (mirroring) until it reaches the threshold of values set for the operator.
On each value
take compares the number of values it has mirrored with the defined threshold. If the threshold is reached, it completes the stream by unsubscribing from the source and passing the complete notification to the observer.
take operator works in the following way:
- Subscribe to a source observable
- When a new value arrives from a source observable, send the value to the observer
- Increase the counter and compare it with the defined threshold
- If these numbers are equal, unsubscribe from the source observable and send the complete notification to the observer
- Once the source observable completes, send the complete notification to the observer
- If the source observable throws an error, send the error notification to the observer
The following diagram demonstrates this sequence of steps:
UsageLink to this section
A common use case for the
take operator is to query a storage that is an observable. For example, when you have a global store implemented with observables, like NgRx or Redux-observable, you will need to get part of that state in an imperative way.
Here’s how you can do it with
const store = new BehaviorSubject(1); // since store here is implemented as a Subject and doesn't complete, // and if we don't use `take(1)`, the subscription is kept forever store.pipe(take(1)).subscribe((v) => console.log(v));