• Complain

Srikanth Machiraju - Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8

Here you can read online Srikanth Machiraju - Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8 full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2018, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Srikanth Machiraju Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8

Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Understand functional programming concepts and the functional nature of the JavaScript language. Starting with an introduction to functional programming concepts, you will learn the key differences between imperative and functional programming. Diving straight into JavaScript functions, you will learn to write them with ES8. Followed by this you will move to higher order functions and learn how Function as Data opens up a world of possibilities.

You will then build higher order functions with closures. Arrays will then be introduced, followed by a set of APIs you can use with them. You will learn how to transform one function to another using currying and partial application. The compose function will be discussed in detail, followed by functors and monads. After having an in-depth look at applicative functors, you will learn the new features offered in ES8.

The concluding chapters of Beginning Functional JavaScript will show you how to use a functional toolkit to build a small library that allows you to develop web applications, followed by tips on testing your functional code.

What You Will Learn

  • Discover functional programming concepts such as string padding and async functions
  • Identify how functions are treated in JavaScript
  • Create a functional library that mimics Underscore.JS
  • Deep dive into ES8 functional features such as spread operators and generators
  • Create a library that works like the react-redux pattern by following the functional paradigm

Who This Book Is For

Novice JavaScript developers.

Srikanth Machiraju: author's other books


Who wrote Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8? Find out the surname, the name of the author of the book and a list of all author's works by series.

Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8 — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Anto Aravinth, Srikanth Machiraju 2018
Anto Aravinth and Srikanth Machiraju Beginning Functional JavaScript https://doi.org/10.1007/978-1-4842-4087-8_10
10. Pause, Resume, and Async with Generators
Anto Aravinth
(1)
Chennai, Tamil Nadu, India
(2)
Hyderabad, Andhra Pradesh, India

We started the book with a simple definition of functions, then we saw how to use functions to do great things using the functional programming technique. We have seen how to handle arrays, objects, and error handling, in pure functional terms. It has been quite a long journey for us, but we still have not talked about yet another important technique that every JavaScript developer should be aware of: asynchronous code.

You have dealt with a great deal of asynchronous codes in your project. You might be wondering whether functional programming can help developers in asynchronous code. The answer is yes and no. The technique that were going to showcase initially is using ES6 Generators and then using Async/Await, which is a new addition to the ECMAScript 2017/ES8 specification. Both the patterns try to solve the same callback problem in their own way, so pay close attention to the subtle differences. Generators were new specs for functions in ES6. Generators are not really a functional programming technique; however, they are part of a function (functional programming is about function, right?); for that reason we have dedicated a chapter to it in this functional programming book.

Even if you are a big fan of Promises (which is a technique for solving the callback problem), we still advise you to have a look at this chapter. You are likely to love generators and the way they solve the async code problems.

Note

The chapter examples and library source code are in branch chap10 . The repos URL is https://github.com/antsmartian/functional-es8.git .

Once you check out the code, please check out branch chap10 :

git checkout -b chap10 origin/chap10

For running the codes, as before run:

...npm run playground...

Async Code and Its Problem

Before we really see what generators are, lets discuss the problem of handling async code in JavaScript in this section. We are going to talk about a callback hell problem. Most of the async code patterns like Generators or Async/Await try to solve the callback hell problem in their own ways. If you already know what it is, feel free to move to the next section. For others, please read on.

Callback Hell
Imagine you have a function like the one shown in Listing .
let sync = () => {
//some operation
//return data
}
let sync2 = () => {
//some operation
//return data
}
let sync3 = () => {
//some operation
//return data
}
Listing 10-1

Synchronous Functions

The functions sync , sync1 , and sync2 do some operations synchronously and return the results. As a result, one can call these functions like this:
result = sync()
result2 = sync2()
result3 = sync3()
What if the operation is asynchronous? Lets see it in action in Listing .
let async = (fn) => {
//some async operation
//call the callback with async operation
fn(/* result data */)
}
let async2 = (fn) => {
//some async operation
//call the callback with async operation
fn(/* result data */)
}
let async3 = (fn) => {
//some async operation
//call the callback with async operation
fn(/* result data */)
}
Listing 10-2

Asynchronous Functions

Synchronous vs. Asynchronous

Synchronous is when the function blocks the caller when it is executing and returns the result once its available.

Asynchronous is when the function doesn't block the caller when its executing the function but returns the result once available.

We deal with Asynchronous heavily when we deal with an AJAX request in our project.

Now if someone wants to process these functions at once, how they do it? The only way to do it is shown in Listing .
async(function(x){
async2(function(y){
async3(function(z){
...
});
});
});
Listing 10-3

Async Functions Calling Example

Oops! You can see in Listing that we are passing many callback functions to our async functions. This little piece of code showcases what callback hell is. Callback hell makes the program harder to understand. Handling errors and bubbling the errors out of callback are tricky and always error prone.

Before ES6 arrived, JavaScript developers used Promises to solve this problem. Promises are great, but given the fact that ES6 introduced generators at a language level, we dont need Promises anymore!

Generators 101

As mentioned, generators were part of the ES6 specifications and they are bundled up at language level. We talked about using generators to help with handling async code . Before we get there, though, we are going to talk about the fundamentals of generators. This section focuses on explaining the core concepts behind generators. Once we learn the basics, we can create a generic function using generators to handle async code in our library. Lets begin.

Creating Generators
Lets start our journey by seeing how to create generators in the first place. Generators are nothing but a function that comes up with its own syntax. A simple generator looks like Listing .
function* gen() {
return 'first generator';
}
Listing 10-4

First Simple Generator

The function gen in Listing is a generator. As you might notice, we have used an asterisk before our function name (in this case gen ) to denote that it is a generator function. We have seen how to create a generator; now lets see how to invoke a generator:
let generatorResult = gen()
What will be the result of generatorResult ? Is it going to be a first generator value? Lets print it on the console and inspect it:
console.log(generatorResult)
The result will be:
gen {[[GeneratorStatus]]: "suspended", [[GeneratorReceiver]]: Window}
Caveats of Generators

The preceding examples show how to create a generator, how to create an instance for it, and how it gets values. There are a few important things we need to take care of, though, while we are working with generators.

The first thing is that we cannot call next as many times as we want to get the value from the generator. To make it clearer, lets try to fetch a value from our first generator (refer to Listing for the first generator definition):
let generatorResult = gen()
//for the first time
generatorResult.next().value
=> 'first generator'
//for the second time
generatorResult.next().value
=> undefined

As you can see in this code, calling next for the second time will return an undefined rather than first generator . The reason is that generators are like sequences: Once the values of the sequence are consumed, you cannot consume it again. In our case generatorResult is a sequence that has value as first generator . With our first call to next , we (as the caller of the generator) have consumed the value from the sequence. Because the sequence is empty now, calling it a second time will return you undefined .

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8»

Look at similar books to Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8»

Discussion, reviews of the book Beginning Functional JavaScript: Uncover the Concepts of Functional Programming with EcmaScript 8 and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.