Async
So far, all of our functional programming has been applied to values that were immediately available to us.
This style of functional programming is synchronous and eager.
How do we apply functional principles and techniques to asynchronous programming?
Map Lazy & Lazy Array
Imagine you could have a mapLazy
function that allowed you to asynchronously map over values in an array as new values were added in.
What's more: what if you could define a LazyArray
data structure? It would almost be like you're creating event listeners as you add/remove values from the lazy array.
Importance: If a LazyArray
existed, since we know we can apply functional techniques to regular arrays, we could in theory apply them to these lazy arrays as well!
Observables
Although lazy arrays don't actually exist, observables are conceptually the same thing.
An observable involves an asynchronous flow of data. Think of like a spreadsheet: you have cell A1 with a value and cell B5 with the formula =A1 * 2
. If you change A1, B5 changes at the same time because B5 is mapped to A1.
Note: Observables fall under the area of reactive programming.
Reactive Programming with Rx.js
Rx.js stands for reactive extensions. Here's how you'd listen for changes to an observable. It's practically identical to our fictitious example above:
Note: When you call any method like map
or filter
or reduce
in an observable, it returns another observable. That's how we're able to subscribe
to the changes to the observable.
Last updated