liveBook · Manning

Observable

As explained in Functional Programming in JavaScript

An object representing a stream of values or events that observers can subscribe to over time.

An observable represents data or events that arrive over time. You subscribe to it to receive its ordered values, whether they come from user input, a file, a web service, a database, notifications, a collection, or a string. section 8.4

Observables make different kinds of data work through the same functional pipeline:

  • Create a stream from values, ranges, promises, generators, or events. section 8.4
  • Transform values with operations such as map and filter, then chain those operations. section 1.3 section 8.4
  • Subscribe to receive each value, handle errors, and respond when the stream completes. section 8.4

Validate an input stream

Instead of putting all SSN handling inside an event callback, create an observable from the input event and process each value through small functions: Rx.Observable.fromEvent(document.querySelector('#student-ssn'), 'change') .map(event => event.target.value) .map(cleanInput) .map(checkLengthSsn) .subscribe(ssn => ssn.isRight ? console.log('Valid') : console.log('Invalid')); The same pattern handles a stream of user input as a sequence that can be transformed and consumed. section 1.3 section 8.4

Rx.Observable.fromEvent(
  document.querySelector('#student-ssn'), 'change')                        #1
   .map(x => x.target.value)                                               #2
   .map(cleanInput)                                                        #3
   .map(checkLengthSsn)                                                    #3
   .subscribe(                                                             #4
     ssn => ssn.isRight ? console.log('Valid') : console.log('Invalid'));

Follow the complete event-stream pipeline from DOM events through extraction, cleaning, validation, and subscription. section 8.4

How Functional Programming in JavaScript covers it

How deep it goes
Defines streams, subscription, transformation, errors, and completion, while deferring broader reactive operations to later treatment.
Examples
Demonstrates numeric ranges, generated squares, odd-number transformation, SSN events, and student data from a promise.
What it assumes
Assumes basic JavaScript functions and events while introducing streams, subscription, and functional transformation patterns.
What you can do after
Apply RxJS to build composable pipelines for asynchronous values and event streams.
How it connects
Connects functional programming, reactive programming, promises, generators, event handling, and function composition.

Also explained in

Compare how 5 books explain observable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.