Hey there! If you've been working with JavaScript and heard about reactive programming, you've probably come across RxJS. It's a powerful library for handling asynchronous operations and event-based programs using observables.
In this notebook, we'll explore the basics of RxJS, focusing on observables - the core building blocks of reactive programming. We'll cover creating observables, subscribing to them, using operators, and handling common patterns. By the end, you'll have a solid foundation to start using RxJS in your projects.
$ npm install rxjs added 2 packages in 2s 28 packages are looking for funding run `npm fund` for details
RxJS loaded successfully!
An Observable is a core concept in RxJS. Think of it as a stream of data that can emit multiple values over time. Unlike Promises that resolve once, Observables can emit multiple values and are lazy - they don't start producing values until you subscribe to them.
Before subscribing - nothing happens yet
Subscribing to the observable: Observable executed! Received: First value Received: Second value
RxJS provides several ways to create observables from different data sources.
Observable from 'of': of emitted: 1 of emitted: 2 of emitted: 3 of emitted: 4 of emitted: 5 of completed
Observable from array: Array emitted: 10 Array emitted: 20 Array emitted: 30 Array emitted: 40 Array emitted: 50 Array observable completed
Observable from promise: Promise emitted: Resolved promise value Promise observable completed
Observable from interval (taking only 5 values):
Operators are functions that let you transform, filter, and combine observables. They're one of the most powerful features of RxJS.
Using map operator to double each value: Doubled: 2 Doubled: 4 Doubled: 6 Doubled: 8 Doubled: 10
Using filter operator to get only even numbers: Even number: 2 Even number: 4
Chaining operators - filter even numbers then multiply by 10: Processing: 20 Final value: 20 Processing: 40 Final value: 40
Observables have built-in error handling capabilities.
Observable with error: Received: Value before error
Using catchError to handle errors gracefully: Received: Value before error Error caught by operator: Something went wrong! Received: Fallback value 1 Received: Fallback value 2 Observable completed successfully
Subjects are special types of Observables that allow values to be multicasted to many Observers. They act as both an Observable and an Observer.
Creating two subscribers to a Subject:
Emitting values to all subscribers: Subscriber 1 received: First value from subject Subscriber 2 received: First value from subject Subscriber 1 received: Second value from subject Subscriber 2 received: Second value from subject Subscriber 1 completed Subscriber 2 completed
Let's see a practical example of using RxJS for data fetching with error handling.
Fetching data for multiple IDs with error handling: Fetching data for id: 1 Fetching data for id: 2 Fetching data for id: 3 Fetching data for id: 4 Fetching data for id: 5
Here's what we've learned about RxJS and Observables:
RxJS is incredibly powerful for handling asynchronous operations, events, and data streams. While there's a learning curve, mastering Observables can greatly simplify complex async code.