RxJS Interview Questions and Answers
Ques 11. Explain the concept of 'rxjs/operators' and its significance.
'rxjs/operators' is a module in RxJS that provides a collection of operators. These operators are used for transforming, filtering, and combining Observables to perform various operations.
Example:
import { map, filter } from 'rxjs/operators'; const modifiedObservable = observable.pipe(map(value => value * 2), filter(value => value > 5));
Ques 12. What is the purpose of the 'switchMap' operator in RxJS?
'switchMap' is used to transform each value emitted by an Observable into a new Observable and switch to emitting values from the latest Observable, ignoring previous ones.
Example:
const switchedObservable = observable.pipe(switchMap(value => of(value, value * 2)));
Ques 13. Explain the concept of 'Subjects' in RxJS.
Subjects are both Observers and Observables. They can multicast values to multiple Observers, making them useful for scenarios where multiple subscribers need to receive the same values.
Example:
const subject = new Subject(); subject.next('First value'); subject.subscribe(value => console.log(value));
Ques 14. What is the purpose of the 'pluck' operator in RxJS?
'pluck' is used to extract a specified property from each emitted object in an Observable stream.
Example:
const pluckedObservable = sourceObservable.pipe(pluck('name'));
Ques 15. Explain the difference between 'concatMap' and 'mergeMap' operators in RxJS.
'concatMap' concatenates the emissions of mapped Observables in the order they were emitted, while 'mergeMap' merges them as soon as they arrive, possibly interleaving values.
Example:
const concatenatedObservable = observable.pipe(concatMap(value => of(value, value * 2)));
Most helpful rated by users: