Note: This article is published on 02/06/2025.

This series of articles will discuss the major feafures for Angular. The way to discuss could be AI in concept + Code Demo in StackBlitz | Instant Dev

Advanced:

A - Introduction

In Angular, both Observable and Promise are used to handle asynchronous operations, but they have key differences in behavior and use cases. The content of this article

B - Differences:

The major differences will be discussed below:

1. Observable (RxJS)

2. Promise

Example of Observable

import { Observable } from 'rxjs';

const observable = new Observable(observer => {
  observer.next("First value");
  observer.next("Second value");
  setTimeout(() => observer.next("Third value after 2s"), 2000);
  observer.complete();
});

observable.subscribe(value => console.log(value));

Example of Promise

const promise = new Promise(resolve => {
  setTimeout(() => resolve("Promise resolved!"), 2000);
});

promise.then(value => console.log(value));

Key Differences Summary

When to Use What?

In Angular, Observables are commonly used with HttpClient and Forms due to their flexibility and cancellation ability.

References: