The Async Duel: Promises vs Observables

Master asynchronous programming in Angular. Learn when to use eager Promises versus lazy, powerful RxJS Observables.

Simulation ProgressStep 1 of 8
0 EXP

Welcome to the timeline. In Angular, we handle future events using two main constructs: Promises and Observables.

// Initializing Async Context...

The Promise

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason.

Crucially, promises are eager. The code inside the constructor executes immediately upon creation, not waiting for a handler to be attached.

Concept Check

When does the code inside a new Promise(...) executor run?

Advanced RxJS Training

0 EXP

Deepen your understanding of reactive programming.


Achievements

Async Initiate

Understand the basic difference between Eager and Lazy execution.

🏄
Stream Surfer

Successfully subscribe to an Observable stream.

🛠️
Operator Expert

Master the syntax of piping operators in RxJS.

Mission: Construct a Data Stream

Create an Observable that emits at least one value using `next()`, then `complete()`s. Finally, `subscribe()` to it.

A.D.A. Feedback:

> Awaiting RxJS implementation...

Challenge: Lifecycle of a Subscription

Drag the events into the correct chronological order of an RxJS stream lifecycle.

Subscribe to Observable
Define Observable
Unsubscribe (Teardown)
Receive Data (.next)

Challenge: Complete the Chain

Fill in the missing RxJS methods to transform data from an HTTP call.

http.get('/api/data').((data => data.id)).(val => log(val));

Consult A.D.A. (Async Data Assistant)

Dev Community Hub

The Asynchronous Duel: Promises vs Observables

In modern web development, particularly within the Angular ecosystem, handling asynchronous operations—like HTTP requests, timers, or user input events—is a daily task. Two primary contenders vie for control over your async logic: the native JavaScript **Promise** and the RxJS **Observable**.

The Fundamental Difference: One vs. Many

The most distinct difference lies in how many values each can produce.

  • Promise: Resolves to a single value (or rejects with an error) and then completes. It is ideal for "one-and-done" operations, like fetching a configuration file at app startup.
  • Observable: Represents a stream of values over time. It can emit zero, one, or infinite values. This makes it perfect for "ongoing" events, like WebSocket messages, mouse movements, or search bar inputs.

Eager vs. Lazy Execution

This is often the source of bugs for beginners.

Promises are Eager

The moment you invoke the Promise constructor, the code inside runs. You cannot stop it from starting.

const p = new Promise(resolve => {
  console.log("I run immediately!");
  resolve(1);
});

Observables are Lazy

Nothing happens when you define an Observable. The code only runs when you call .subscribe().

const obs$ = new Observable(obs => {
  console.log("I wait for subscription");
  obs.next(1);
});

Cancellability

Promises generally cannot be cancelled. Once fired, the request goes out. Observables, however, return a Subscription object. Calling .unsubscribe() on this object cancels the ongoing operation. This is critical in Single Page Applications (SPAs) to prevent memory leaks when a user navigates away from a component before an async task finishes.

Pro Tip: Angular's HttpClient returns Observables. Use the async pipe in your templates to handle subscription and unsubscription automatically!

RxJS & Async Glossary

Promise
An object representing the eventual completion or failure of an asynchronous operation. It holds a single future value.
Observable
A blueprint for creating streams of data. It can emit multiple values over time and is lazy (cold) by default.
Observer
A consumer of values delivered by an Observable. It is an object with `next`, `error`, and `complete` callbacks.
Subscription
An object representing the execution of an Observable. It is primarily used to cancel the execution via `.unsubscribe()`.
Operator
Functions used to transform, filter, or combine Observables (e.g., `map`, `filter`, `switchMap`).
Stream
A sequence of data elements made available over time.

About the Author

Author's Avatar

TodoTutorial Team

Angular experts dedicated to simplifying reactive programming.

This lesson is curated by senior frontend developers with extensive experience in building large-scale Angular applications using RxJS.

Verification and Updates

Last reviewed: October 2025.

Ensured compatibility with Angular 18+ and RxJS 7+.

External Resources

Found an error or have a suggestion? Contact us!