Interview Questions
Q: What is RxJS?
RxJS is a library for reactive Streams which can be used to deal with asynchronous data streams and events called "Reactive Extensions for JavaScript" (RxJS).
RxJS uses JavaScript reactive programming. RxJS is very popular because it makes writing asynchronous code simple for developers.
Q: What is Stream?
A stream is a key part of reactive programming. In simple terms,
"A stream refers to values of data overtime"
The reason for it being called a stream is that you should think of the data as continuous and not really having an end, unless you explicitly define an end.
Q: What is Observable?
"Observable represents the idea of an invokable collection of future values or events."
In RxJS, you model asynchronous data streams using observable sequences Or just called observables, too. An Observable is an object that over time and asynchronously emits multiple data values (data stream).
Q: What is the difference between an observable and a promise?
Promise:
A Promise emits a single event at the completion or failure of an async operation.
promise emits a single value
A promise is Not Lazy A Promise cannot be cancelled
Observable:
An observer is like a stream and allows you to pass at least zero or more events where the callback is needed for each event. Observable is favored over Promise Emits multiple values over a time. The "Observable" is cold. It's not called until we're registered to it. You may cancel an Observable with the unsubscribe() method. Observable provides a lot of efficient operators like map, foreach, filter, reduce, retry, retryWhen etc.
Q: What is the difference between Cold and Hot Observables?
"Cold observables start to run in up and subscription, so observable sequence only starts pushing values to observers when subscribe is called. You can use cold observables if you don't want to produce data before any observer is subscribed, for example Ajax call.
"Hot observables produce values even before subscriptions are made.
Hot observables such as mousemove events, stock pickers or WebSocket connections, are already produced in values even before subscription is active. If you want to share some resources or data between many subscribers, you have to use hot observables.
Q: What are RxJS Operators ?
A stream is a key part of reactive programming. In simple terms, "An operator is simply a method that acts on an Observable and changes the stream in some way. The purpose of operator, to modify or filter originally emitted values in the way we need for the project tasks.
Q: What are Observers and Subscriptions ?
Observers:
Observer is a set of callbacks that know how to listen to the values of the Observable. Observers are also referred to as listeners (or consumers), Observers may listen or subscribe to the data being observed.
Subscription:
Subscription is an observable execution.
Subscriptions are objects returned when an Observable is subscribed. Subscription is useful mainly to cancel the execution.
Q: What is Subject?
Subjects are special types of Observers, so you can also subscribe to other Observables and listen to published data. Special thing about the subject is that they are multicasted. It means - "The values are multicasted to many Observers" while default RxJS Observable is unicast
Q: What are different types of Subject ?
There are two types of Subjects : BehaviorSubject and ReplaySubject.
Q: What are the differences between Subject, BehaviorSubject and ReplaySubject?
Subject:
In case of Subject, Observers who are subscribed at a later date will not obtain the data values emitted prior to their subscription.
ReplaySubject:
In ReplaySubject, Observers who are subscribed at a later point will receive data values issued prior to their subscription. As it operates by using a buffer that holds the values emitted and re-emits them once new Observers are subscribed.
BehaviorSubject:
BehaviorSubject functions similar to ReplaySubject but only re-issues the last emitted value. So you're interested in the last / current value of the observer, if BehaviorSubject is useful.
Q: What is Reactive programming and how does it relate to Angular?
The principle of responsive programming is that you can just build the different streams and operations that take place on those flows by specifying the whole program.
Angular uses RxJS for some aspects of its internal service, such as Http, Router, etc. RxJS is a very powerful library that facilitates the design of applications.
Q: What is RxJS Map and What is Higher-Order Observable Mapping?
RxJS map operator lets us project the payload of the Observable into something else.The power of Observables is revealed when you start using Rx operators to transform, combine, manipulate, and work with sequences of items emitted by Observables.
Q: When do we use the switchMap, mergeMap and concatMap?
concatMap(), mergeMap(), switchMap() and exhaustMap(). All of these operators are flattening operators used to flatten observables, but they are applicable in very different scenarios. switchMap and mergeMap are probably going to be the most powerful and frequently used operators. It is thereby critical to understand the difference between the two in order to spend less time debugging code.
Q: What is RxJS concatMap?
concatMap : Projects each source value to an Observable which is merged in the output Observable, in a serialized fashion waiting for each one to complete before merging the next - Official RxJS Docs
Q: What is RxJS mergeMap?
mergeMap : mergeMap allows for multiple inner subscriptions to be active at a time. Because of this, one of the most common use-case for mergeMap is requests that should not be canceled.
Q: What is RxJS switchMap?
switchMap : Simply put, it means switching to a new observable. The previous inner observable (result of the function you provided) is canceled for each emission and the new observable is subscribed.
Q: When we use zip and combineLatest and withLatestFrom?
zip and combineLatest are functions and withLatestFrom is an operator that allows to combine a few observable sequences in different ways that are really helpful in real world application. Composing functions and operators usually accept observables as their params and also they return observables that emit arrays with values produced by argument observables. This result observable emission logic is different depending on which operator or function we use.
Last updated