Angular 9 Interview notes

DHEERAJ KUMAR
3 min readJul 4, 2020

here are some of the questions that you might need to know before your next Angular Interview

Q1 What is Observer in Angular?
Well, Observer is an object passed to the subscribe() method for an observable. The object defines the callbacks for the subscriber.

Q2 Can you describe Observable in Angular?
Observable is the producer of multiple values, which it pushes to subscribers. It is used for asynchronous event handling throughout Angular.

An observable can be executed by subscribing to it with its subscribe() method, passing callbacks for notifications of new values, errors, or completion.

Observables can potentially deliver single or multiple values of any type to subscribers, either synchronously (as a function delivers a value to its caller) or on a schedule.

A subscriber receives notification of new values as they are produced and notification of either normal completion or error completion.

Angular uses a third-party library called Reactive Extensions (RxJS).

Q3 What is promiser in Angular?
In Angular there are two ways to handle asynchronous functions one is via Promises and the other via Observables.

In ES6 and TypeScript we can use promises by returning an instance of a Promise class.

Promises deal with asynchronous calls mostly static, which are useful for making API requests, for example.

Promises have a major limitation in that they’re only useful for one call cycle.

Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time.

Code Snippet:
search(term:string) {
let promise = new Promise((resolve, reject) => {
let apiURL = `${this.apiRoot}?term=${term}&media=music&limit=20`;
this.http.get(apiURL) <! — in ng http returns observables — >
.toPromise()
.then(
res => { // Success
console.log(res.json());
resolve();
} );
});
return promise;
}

Q4 What are the key differences between observable and promise?

I had listed four of the key differences below:

i- Observables are declarative & computation does not start until subscription. But Promises execute immediately upon creation.

ii- Observables provides many values making them useful for getting multiple values over time. Whereas, Promises provide only one value at a time and mostly static.

iii- Observables differentiate between chaining and subscription making them useful for creating complex transformation recipes to be used by other part of the system, without causing the work to be executed. In case of Promises they only have .then() clauses

iv- Observables subscribe() is responsible for handling errors making them useful for centralized and predictable error handling. Whereas, Promises push errors to the child promises.

Q5 What are the different types of Observables?

The type differ between themselves based on how they react to subscribe().

In RxJs there are 4 types of Subject that provides us with to create an Observable, namely:

i- Subject

When you subscribe to a Subject, you get every event that this Subject emits after you have subscribed (including complete). With a Subject, the Observer will never be notified of events before subscribe.

ii- BehaviorSubject

Its’ like a Subject, except the Observer also receives the last event that occurred before the subscription. It then receives all the events occurring after the subscription, like for a regular Subject.

iii- ReplaySubject

In this the Observer receives all past events when it subscribes. It then receives all the events occurring after the subscription, like for a regular Subject.

iv- AsyncSubject

This one has a peculiar behavior. AsyncSubject will wait for complete to emit the last event and then the complete event.

--

--

DHEERAJ KUMAR

.Net Core Expert and an Experienced Full Stack Engineer (nodejs+Angular 9) with a demonstrated history of working in the product development software company.