Asynchronous functional javascript
Asynchronous functional javascript
About the Book
This is a book about asynchronous javascript from the unifying perspective of category theory and functional programming.
A 5 installment is in progress that will include a chapter in Reactive Extensions with Rx.js.
Note: This is not an Introductory book in Asynchronous Javascript. The reader must have at least a couple of years of Javascript experience. Please read the Sample pdf to get an idea about the content of the book.
included Js source code for selected sections like Fluture.js and Folktale.js
Table of Contents
-
1 Introduction to Asynchrony
- 1.1 Concurrency, Parallelism, Asynchrony ... 12
- 1.1.1 Concurrency ... 12
- 1.1.2 Parallelism ... 12
- 1.1.3 Threads ... 12
- 1.1.4 Synchronous and Asynchronous ... 13
- 1.2 The Problem of Asynchrony ... 14
- 1.3 The JavaScript Event Loop ... 14
- 1.4 Callbacks ... 18
- 1.5 The Continuation monad ... 18
- 1.6 Promises ... 19
- 1.7 Async-Await ... 19
- 1.8 Events ... 20
- 1.9 The basic Criteria of Comparison ... 22
-
2 Callbacks
- 2.1 Continuation Passing Style Transformation ... 24
- 2.2 Continuations and Callbacks ... 25
- 2.3 Callbacks Example. ... 26
- 2.4 Including Error Handling ... 28
- 2.5 The Continuation (a → r) → r Type ... 29
-
- 2.6 Applying a function ... 32
- 2.6.1 Callbacks ... 33
- 2.6.2 The Continuation ... 34
- 2.7 Sequential and Parallel execution ... 34
-
3 Why Functional Programming
- 3.1 Categories ... 35
- 3.1.1 One-minute Functors ... 37
- 3.1.2 The Identity Functor ... 38
- 3.1.3 The Basic Functor Mechanics ... 39
- 3.1.4 A brief mentioning of Pattern Matching ... 39
- 3.1.5 The Asynchronous identity functor ... 40
- 3.1.6 Pattern Matching for Hierarchies ... 41
- 3.1.7 Adding Pattern Matching extensions Hierarchies ... 42
- 3.1.8 Rewriting Subtype methods with matchWith ... 42
- 3.1.9 Pattern Matching on Callbacks? ... 43
- 3.1.10 Pattern Matching on Continuations? ... 45
- 3.2 Either Functor ... 45
- 3.2.1 Using Either for exception handling ... 47
- 3.2.2 Either Functor Example ... 49
- 3.3 The one Minute introduction to monads ... 50
- 3.4 Either Monad ... 51
- 3.4.1 Using Either Monad exception handling ... 52
- 3.5 Showcase – Bind for Some Async Structures ... 54
- 3.5.1 Monoids ... 54
- 3.5.2 Multiplying stuff ... 55
- 3.6 Composing Continuations ... 58
- 3.6.1 The Continuation monad ... 59
- 3.7 Folding Callbacks and Continuations ... 60
-
4 Promises
- 4.2 Creating promises ... 64
- 4.2.1 The Promise executor as Union Type ... 64
- 4.3 Extending Custom Promise to a Functor ... 66
- 4.4 Extending Custom Promise to Monad ... 67
- 4.5 Promisify Callback code ... 68
- 4.6 Dissecting the Promise.then method ... 69
- 4.6.1 The then as map ... 70
- 4.6.2 The then as a monadic Bind ... 71
- 4.6.3 The (then, catch) as pattern matching ... 72
- 4.6.4 Conclusion ... 74
- 4.6.5 Another Example of the monadic composition ... 74
- 4.6.6 Promises and the Either Monad ... 76
- 4.7 Parallel Execution ... 79
- 4.7.1 Promise.all ... 81
- 4.7.2 Promise.all Example ... 83
- 4.7.3 Promise.race ... 84
- 4.8 Thenables and PromiseLike ... 84
- 4.9 Combining Promises and Either ... 85
- 4.10 Combining Promises and Either - Example ... 87
- 6.1.1 Error handling ... 98
-
5 Fluture.js and Futures... 90
-
6 Folktale.js and Tasks ... 93
-
6 Web Workers for Parallelism ... 93
- 6.1.2 Abstracting Workers with Promises ... 98
- 6.2 Monoids ... 99
- 6.2.1 Folding monoids ... 102
- 6.2.2 Monoid homomorphisms and Parallelism ... 102
-
7 Async-Await
- 7.1 Async ... 109
- 7.2 The await keyword ... 110
- 7.3 Error handling ... 111
- 7.4 Refactoring Promises to Async/await ... 111
- 7.5 Sequential and Parallel execution ... 113
- 7.5.1 Promise.all Example refactoring ... 114
-
8 Apendix A- Algebraic Data Types
- 8.1 The product structure: ... 117
- 8.1.1 Introduction / Elimination ... 118
- 8.2 The co-product (aka Union) structure: ... 119
- 8.2.1 Introduction / Elimination ... 121
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them