Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering Reactive JavaScript

You're reading from   Mastering Reactive JavaScript Building asynchronous and high performing web apps with RxJS

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781786463388
Length 310 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Erich de Souza Oliveira Erich de Souza Oliveira
Author Profile Icon Erich de Souza Oliveira
Erich de Souza Oliveira
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

1. What Does Being Reactive Mean? FREE CHAPTER 2. Reacting for the First Time 3. A World Full of Changes - Reactive Extensions to the Rescue 4. Transforming Data - Map, Filter, and Reduce 5. The World Changes Too Fast - Operators to Deal with Backpressure 6. Too Many Sources - Combining Observables 7. Something is Wrong - Testing and Dealing with Errors 8. More about Operators 9. Composition 10. A Real-Time Server 11. A Real-Time Client

Subscribing to changes (Observer)


To listen to data on an observable, we must call the subscribe() method. This method returns a subscription, which we can use later to stop reacting to the incoming data if we are no longer interested in it.

The subscribe() method of observables has the following signature:

observable.subscribe(onNext,onError,onCompleted); 

All parameters are optional and can be omitted if we are not interested in this type of event:

  • onNext: This is a function to be called every time new data is propagated through the observable
  • onError: This is a function to be called every time an error occurs in the observable
  • onCompleted: This is a function to be called when the observable is completed

The easiest way to subscribe to an observable is to just pass the onNext parameter (as we have been doing in most of our code snippets in this chapter):

Rx.Observable 
    .just('Hello World!!!') 
    .subscribe((message)=>console.log(message)); 

If you run the preceding code, it will print...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images