Interview Questions

1. What do you understand by observable and observer in Angular?

Observable: An observable is a unique object just like a promise that is used to manage async code. Observables are not part of the JavaScript language so the developers have to rely on a popular Observable library called RxJS. The observables are created using the new keyword.

See a simple example of observable to understand it better:

import { Observable } from 'rxjs';  

const observable = new Observable(observer => {  

  setTimeout(() => {  

    observer.next('This is a message from Observable!');  

  }, 1000);  

});

Observer: Any object that has to be notified when the state of another object changes is called an observer. An observer is an interface for push-based notifications delivered by an Observable.

See the structure of an observer:

interface Observer<T> {  

  closed?: boolean;  

  next: (value: T) => void;  

  error: (err: any) => void;  

  complete: () => void;  

 

The handler that implements the observer interface for receiving observable notifications is passed as a parameter for observable as follows:

myObservable.subscribe(myObserver);  


By Md Riyazuddin 2 0
Is this helpful? Yes No

Submit an interview question

Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of w3Sniff.

Get Started

Comments

Leave a Comment


Check out more interview questions

Based on your skills

MS SQL Server

1648361 1720 23 859

Angular

41914 92 0 46

Wordpress

33975 72 1 36