Reactive Programming with RxJS Untangle Your Asynchronous JavaScript Code 1st Edition Sergi Mansilla - The latest updated ebook version is ready for download
Reactive Programming with RxJS Untangle Your Asynchronous JavaScript Code 1st Edition Sergi Mansilla - The latest updated ebook version is ready for download
https://ebookfinal.com/download/html5-programming-with-javascript-for-
dummies-mueller/
https://ebookfinal.com/download/asynchronous-android-programming-2nd-
edition-helder-vasconcelos/
https://ebookfinal.com/download/node-up-and-running-scalable-server-
side-code-with-javascript-1st-edition-tom-hughes-croucher/
https://ebookfinal.com/download/programming-reactive-extensions-and-
linq-1st-edition-jesse-liberty/
Learn Unity3D Programming with UnityScript Unity s
JavaScript for Beginners 1st Edition Janine Suvak
https://ebookfinal.com/download/learn-unity3d-programming-with-
unityscript-unity-s-javascript-for-beginners-1st-edition-janine-suvak/
https://ebookfinal.com/download/modern-c-programming-with-test-driven-
development-code-better-sleep-better-1st-edition-jeff-langr/
https://ebookfinal.com/download/handbook-of-asynchronous-machines-
with-variable-speed-1st-edition-hubert-razik/
https://ebookfinal.com/download/learn-game-programming-with-ruby-
bring-your-ideas-to-life-with-gosu-mark-sobkowicz/
https://ebookfinal.com/download/getting-started-with-advanced-c-
upgrade-your-programming-skills-1st-edition-vaskaran-sarcar/
Reactive Programming with RxJS Untangle Your
Asynchronous JavaScript Code 1st Edition Sergi Mansilla
Digital Instant Download
Author(s): Sergi Mansilla
ISBN(s): 9781680501292, 1680501291
Edition: 1
File Details: PDF, 3.65 MB
Year: 2015
Language: english
Early praise for Reactive Programming with RxJS
This book is as hot as reactive programming itself! With great writing, clear expla-
nations, and practical examples, this is a fantastic resource for learning RxJS.
➤ Fred Daoud
Software-development contractor
Be proactive and learn reactive programming with this book before it’s too late.
Rx.Observable.fromBook(book).subscribe(function(value) {...do amazing stuff...});
➤ Javier Collado Cabeza
Senior software developer, NowSecure, Inc.
A very readable book with great content. This book is eminently useful and provides
a clear roadmap for learning reactive programming with RxJS with practical ex-
amples.
➤ Ramaninder Singh Jhajj
Software engineer, Area Services & Development, Know-Center, Austria
We've left this page blank to
make the page numbers the
same in the electronic and
paper books.
Sergi Mansilla
Acknowledgments . . . . . . . . . . . ix
Preface . . . . . . . . . . . . . . xi
Index . . . . . . . . . . . . . . 119
Acknowledgments
I have so many people to thank. There are those who have helped shape the
book and those who have helped shape me as a person. I couldn’t have done
this without any of them. I would particularly like to thank the following:
The exceptional people who came up with the Reactive Extensions library in
the first place, and the ones who expanded and evangelized it. This book
would obviously not exist without you: Erik Meijer, Matt Podwysocki, Bart
De Smet, Wes Dyer, Jafar Husain, André Staltz, and many more I am probably
forgetting.
The folks at The Pragmatic Bookshelf. It has been a pleasure to work with
you. Special thanks to Susannah Pfalzer, who has believed in the book since
it was nothing but an idea. I was also extremely lucky to get Rebecca Gulick
as my editor: You have been professional, patient, attentive to my questions,
and a joy to work with. I’ve been a fan of Pragmatic’s books for a long time,
and it has been a privilege to write a PragProg book myself. And, yes, both
publishers, Dave Thomas and Andy Hunt, do read and review every book!
The brilliant technical reviewers. David Bock, Javier Collado Cabeza, Fred
Daoud, Irakli Gozalishvili, Zef Hemel, Ramaninder Singh Jhajj, Aaron Kalair,
Daniel Lamb, Brian Schau, and Stephen Wolff, as well as Pragmatic publishers
Dave and Andy: This book is infinitely better thanks to all of you. You each
selflessly put time and energy into reviewing this book, detecting complicated
errors and saving me from more than one embarrassing mistake. Any errors
remaining in the book are my own fault.
To my friends. The ones who are always there, no matter the time and the
distance; you know who you are. Thanks for the laughs, the support, the
love.
My parents, Narcís Mansilla and Joana Molins. You are my guides and role
models. You never ceased to believe in me and always encouraged me to take
on bigger challenges. You bought me my first computer at a time when you
struggled to pay the bills. That started it all, and I owe you everything.
My son, Adrià. You were born while I was writing this book, and you have
changed the meaning of life for me. You’ve already taught me so much in
such little time, and I can’t wait to see what’s next.
Finally, Jen, the love of my life. You have had infinite patience and supported
me while I wrote a book in one of the busiest periods of our life so far. You
are an inspiration to me and you make me a better human being in every
way. You are my star.
Sergi Mansilla
Barcelona, December 2015
Most software today deals with data that’s available only over time: websites
load remote resources and respond to complex user interactions, servers are
distributed across multiple physical locations, and people have mobile devices
that they expect to work at all times, whether on high-speed Wi-Fi or spotty
cellular networks. Any serious application involves many moving asynchronous
parts that need to be efficiently coordinated, and that’s very hard with today’s
programming techniques. On top of that, we have what’s always been there:
servers crashing, slow networks, and software bugs we have to deal with.
And yet we’re still using good ol‘ imperative-style programming to deal with
problems that are essentially asynchronous. This is very hard.
1. http://venturebeat.com/2012/01/24/why-walmart-is-using-node-js/, http://techblog.netflix.com/2014/06/scale-
and-performance-of-large.html
Callback Functions
A callback is a function (A) passed as a parameter to another function (B) that
performs an asynchronous operation. When (B) is done, it calls back (A) with
the results of the operation. Callbacks are used to manage asynchronous
flows such as network I/O, database access, or user input.
intro/callback_example.js
function B(callback) {
// Do operation that takes some time
callback('Done!');
}
function A(message) {
console.log(message);
}
Callbacks are easy to grasp and have become the default way of handling
asynchronous data flows in JavaScript. But this simplicity comes at a price.
Callbacks have the following drawbacks:
• Callback hell. It’s easy to end up with lots of nested callbacks when han-
dling highly asynchronous code. When that happens, code stops being
linear and becomes hard to reason about. Whole applications end up
passed around in callbacks, and they become difficult to maintain and
debug.
• Callbacks can run more than once. There’s no guarantee the same callback
will be called only once. Multiple invocations can be hard to detect and
can result in errors and general mayhem in your application.
Promises
Promises came to save us from callbacks. A promise represents the result of
an asynchronous operation. In promise-based code, calling an asynchronous
function immediately returns a “promise” that will eventually be either resolved
with the result of the operation or rejected with an error. In the meantime,
the pending promise can be used as a placeholder for the final value.
Event Emitters
When we emit an event, event listeners that are subscribed to it will fire.
Using events is a great way to decouple functionality, and in JavaScript, event
programming is common and generally a good practice.
But, you guessed it, event listeners come with their own set of problems, too:
• Events force side effects. Event listener functions always ignore their
return values, which forces the listener to have side effects if it wants to
have any impact in the world.
• Events are not first-class values. For example, a series of click events can’t
be passed as a parameter or manipulated as the sequence it actually is.
We’re limited to handling each event individually, and only after the event
happens.
Since these mechanisms are what we’ve always used to manage concurrency,
it might be hard to think of a better way. But in this book I’ll show you one:
reactive programming and RxJS try to solve all these problems with some
new concepts and mechanisms to make asynchronous programming a breeze
—and much more fun.
What Is RxJS?
RxJS is a JavaScript implementation of the Reactive Extensions, or Rx.2 Rx
is a reactive programming model originally created at Microsoft that allows
developers to easily compose asynchronous streams of data. It provides a
common interface to combine and transform data from wildly different sources,
such as filesystem operations, user interaction, and social-network updates.
2. https://rx.codeplex.com/
We’ll be developing mostly for the browser, but we’ll see some examples in
Node.js, too. We’ll get deep into the subject early on, and we’ll build applica-
tions along the way to keep it real. Here are the chapters:
Unless you have used RxJS before, start with Chapter 1, The Reactive Way,
on page 1. In this chapter we introduce Observables, the main data type of
RxJS, which we’ll use extensively throughout the book.
We get into some more advanced concepts of RxJS with Chapter 5, Bending
Time with Schedulers, on page 89, where we talk about the useful concept
RxJS provides to handle concurrency at a more fine-grained level: Schedulers.
With the knowledge of Schedulers under our hats, we explore how they help
us with testing. We’ll see how to simulate time in our tests to accurately test
asynchronous programs.
Keep in mind that it is a relatively big file and you may want to consider a
smaller file, such as rx.js or rx.lite.js, for your projects if you’re not using all the
functionality in RxJS.
After that, you can import the RxJS library in your JavaScript files:
var Rx = require('rx');
Rx.Observable.just('Hello World!').subscribe(function(value) {
console.log(value);
});
And you can run it by simply invoking node and the name of the file:
$ node test.js
Hello World!
3. https://github.com/Reactive-Extensions/RxJS/tree/master/dist
RxJS Version
All the examples are made for RxJS 4.x. You can download the latest version
in the RxJS online repository.4
Resources
RxJS is gaining adoption very quickly, and there are more and more resources
about it every day. At times it might be hard to find resources about it online,
though. These are my favorite ones:
4. https://github.com/Reactive-Extensions/RxJS/releases/latest
5. https://github.com/Reactive-Extensions/RxJS
6. http://reactivex.io
7. http://rxmarbles.com/
8. http://pragprog.com/titles/smreactjs
What’s Reactive?
Let’s start by looking at a little reactive RxJS program. This program needs
to retrieve data from different sources with the click of a button, and it has
the following requirements:
• It must unify data from two different locations that use different JSON
structures.
• To avoid requesting data too many times, the user should not be able to
click the button more than once per second.
With all its broad humor, the satire is aflame with indignation. In this
respect the poem performed an important public service. In place of
stupid content with things as they were, it offered critical comment on
existing conditions, comment somewhat biassed, it is true, but
nevertheless in refreshing contrast to the conventional submission of
the great majority of the British public.
Much of what has already been pointed out with regard to the
sources and inspiration of Don Juan may be applied without
alteration to The Vision of Judgment, which is, as Byron told Moore,
written “in the Pulci style, which the fools in England think was
369
invented by Whistlecraft—it is as old as the hills in Italy.” The
Vision, being shorter and more unified, contains few digressions
which do not bear directly upon the plot; but it has the same
colloquial and conversational style, the same occasional rise into
true imaginative poetry with the inevitable following drop into the
commonplace, the same fondness for realism, and the same broad
370
burlesque. Hampered as it is by the necessity of keeping the
story well-knit, Byron’s personality has ample opportunity for
expression.
It is probable that Byron’s description of Saint Peter and the
371
angels owes much to his reading of Pulci. In at least one instance
there is a palpable imitation. Saint Peter in the Vision, who was so
terrified by the approach of Lucifer that,
suffered as did the same saint in the Morgante Maggiore who was
weary with the duty of opening the celestial gate for slaughtered
Christians:
“Credo che molto quel giorno s’affana:
E converrà ch’egli abbi buono orecchio,
Tanto gridavan quello anime Osanna
Ch’eran portate dagli angeli in cielo;
373
Sicchè la barba gli sudava e ’l pelo.”
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com