What Is the Difference Between Observable and Promise

πŸ’‘ Concept Name

Observable vs Promise – Two approaches to handling asynchronous operations in JavaScript and Angular, differing in execution model, features, and flexibility.

πŸ“˜ Quick Intro

While both Observables and Promises help manage asynchronous data, Observables (from RxJS) offer more powerful, flexible features such as multiple emissions, cancellation, and operators, whereas Promises are simpler and resolve once.

🧠 Analogy / Short Story

A Promise is like ordering a meal onceβ€”it delivers a single result. An Observable is like subscribing to a magazineβ€”it delivers multiple issues over time, and you can unsubscribe anytime.

πŸ”§ Technical Explanation

  • πŸ” Observables support multiple values over time; Promises resolve once.
  • πŸ•’ Promises are eagerβ€”start immediately. Observables are lazyβ€”start on subscription.
  • πŸ”„ Observables can be canceled via unsubscribe(); Promises cannot be canceled.
  • πŸ”Œ Observables offer rich operators like map, filter, retry, etc.
  • πŸ”— Angular’s HttpClient returns Observables by default for better stream control.

🎯 Purpose & Use Case

  • βœ… Use Promise for one-time async operations like file read or basic API call.
  • βœ… Use Observable for streams like real-time updates, UI events, or HTTP retries.
  • βœ… Use RxJS operators to transform, debounce, or merge data streams.

πŸ’» Real Code Example

// Using Promise
getUserData(): Promise<any> {
  return fetch('/api/user').then(res => res.json());
}

// Using Observable (RxJS)
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

constructor(private http: HttpClient) {}

getUserData(): Observable<any> {
  return this.http.get('/api/user');
}

// Subscribing to Observable
this.getUserData().subscribe(data => console.log(data));

❓ Interview Q&A

Q1: Can Observables emit multiple values?
A: Yes, unlike Promises which resolve only once.

Q2: Are Promises cancellable?
A: No, once initiated, they run to completion.

Q3: Which is lazy: Promise or Observable?
A: Observables are lazy and only execute when subscribed.

Q4: Which Angular module returns Observables?
A: HttpClient module.

Q5: When would you prefer a Promise over an Observable?
A: For simple, one-time asynchronous actions without stream complexity.

πŸ“ MCQs

Q1. Which emits multiple values over time?

  • Promise
  • Observable
  • Callback
  • None

Q2. Which one is lazy?

  • Promise
  • Observable
  • Both
  • Neither

Q3. Which has unsubscribe() functionality?

  • Promise
  • Observable
  • Async/Await
  • Callback

Q4. Which Angular API returns Observables?

  • Router
  • Forms
  • HttpClient
  • CommonModule

Q5. What is returned by fetch() in JS?

  • Observable
  • Promise
  • Callback
  • Subject

Q6. Which has richer chaining operators?

  • Promise
  • Observable
  • Both
  • None

Q7. What is the default state of a Promise?

  • Resolved
  • Rejected
  • Cancelled
  • Pending

Q8. Which is used with subscribe()?

  • Promise
  • Observable
  • Callback
  • SetTimeout

Q9. Can Observables be canceled?

  • No
  • Yes
  • Only in Angular
  • Only with retry()

Q10. Which is better for real-time updates?

  • Promise
  • Observable
  • AsyncPipe
  • EventEmitter

πŸ’‘ Bonus Insight

Use Angular's async pipe in templates with Observables to auto-subscribe and manage cleanup. This eliminates manual subscriptions and improves performance.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More About Angular πŸš€

What is Angular vs AngularJS πŸ‘‰ Explained
Key Features of Angular πŸ‘‰ Explained
Angular Architecture πŸ‘‰ Explained
Components in Angular πŸ‘‰ Explained
Modules in Angular πŸ‘‰ Explained
Templates in Angular πŸ‘‰ Explained
Directives in Angular πŸ‘‰ Explained
Structural vs Attribute Directives πŸ‘‰ Explained
Services in Angular πŸ‘‰ Explained
Dependency Injection in Angular πŸ‘‰ Explained
Data Binding in Angular πŸ‘‰ Explained
Interpolation πŸ‘‰ Explained
Property Binding πŸ‘‰ Explained
Event Binding πŸ‘‰ Explained
Two-way Data Binding πŸ‘‰ Explained
ngModel Usage πŸ‘‰ Explained
Lifecycle Hooks πŸ‘‰ Explained
ngOnInit Hook πŸ‘‰ Explained
ngOnDestroy Hook πŸ‘‰ Explained
Pipes in Angular πŸ‘‰ Explained
Pure vs Impure Pipes πŸ‘‰ Explained
Create Custom Pipe πŸ‘‰ Explained
Angular Forms πŸ‘‰ Explained
Template-driven vs Reactive Forms πŸ‘‰ Explained
Form Controls πŸ‘‰ Explained
Form Validation πŸ‘‰ Explained
Validation Error Messages πŸ‘‰ Explained
FormGroup vs FormControl πŸ‘‰ Explained
Enable/Disable Form Fields πŸ‘‰ Explained
Reset Form πŸ‘‰ Explained
Routing in Angular πŸ‘‰ Explained
RouterModule πŸ‘‰ Explained
Configure Routes πŸ‘‰ Explained
Route Guards πŸ‘‰ Explained
CanActivate vs CanDeactivate πŸ‘‰ Explained
Lazy Loading πŸ‘‰ Explained
Preloading in Routing πŸ‘‰ Explained
Wildcard Routes πŸ‘‰ Explained
Route Parameters πŸ‘‰ Explained
Query Parameters πŸ‘‰ Explained
Programmatic Navigation πŸ‘‰ Explained
routerLink & routerLinkActive πŸ‘‰ Explained
Resolvers in Routing πŸ‘‰ Explained
Observables in Angular πŸ‘‰ Explained
RxJS in Angular πŸ‘‰ Explained
Observable vs Promise πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup