RxJS Interview Questions and Answers
Ques 21. What is the purpose of the 'finalize' operator in RxJS?
'finalize' is used to perform a specified action when an Observable completes, whether it completes normally or due to an error.
Example:
const finalizedObservable = observable.pipe(finalize(() => console.log('Observable completed')));
Ques 22. Explain the concept of 'ReplaySubjects' in RxJS.
ReplaySubjects are similar to Subjects but can 'replay' a specified number of previously emitted values to new subscribers, ensuring they receive a specific history of values.
Example:
const replaySubject = new ReplaySubject(2); replaySubject.next('First value'); replaySubject.next('Second value'); replaySubject.subscribe(value => console.log(value));
Ques 23. What is the purpose of the 'bufferTime' operator in RxJS?
'bufferTime' is used to collect values emitted by an Observable over a specified time period into arrays and emit these arrays at regular intervals.
Example:
const bufferedObservable = observable.pipe(bufferTime(1000));
Ques 24. Explain the difference between 'BehaviorSubjects' and 'ReplaySubjects' in RxJS.
BehaviorSubjects always emit the latest value to new subscribers, while ReplaySubjects can replay a specified number of previous values to new subscribers.
Example:
const behaviorSubject = new BehaviorSubject('Initial value'); behaviorSubject.next('Updated value'); behaviorSubject.subscribe(value => console.log(value));
Ques 25. What is the purpose of the 'debounce' operator in RxJS?
'debounce' is used to discard values emitted by an Observable that follow each other too quickly, allowing only the last value within a specified time window to be emitted.
Example:
const debouncedObservable = inputObservable.pipe(debounce(() => timer(300)));
Most helpful rated by users: