• Complain

Anto Aravinth [Anto Aravinth] - Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6

Here you can read online Anto Aravinth [Anto Aravinth] - Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6 full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2017, 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.

Anto Aravinth [Anto Aravinth] Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6
  • Book:
    Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6
  • Author:
  • Publisher:
    Apress
  • Genre:
  • Year:
    2017
  • Rating:
    5 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 100
    • 1
    • 2
    • 3
    • 4
    • 5

Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6: 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: Functional Programming with JavaScript Using EcmaScript 6" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Learn functional programming concepts using JavaScript ES6. You will learn concepts such as currying, partial functions, higher-order functions, and monads.

Programming languages have evolved from focusing on procedures to objects and now on function. JavaScript supports functional programming and allows developers to write well-crafted code.

What You Will Learn:

  • Master functional programming concepts
  • Identify how functions are treated in JavaScript
  • Understand real-world functional libraries and create a functional library that mimics underscore.js
  • Perform pure-error handling techniques such as functors and monads
  • Discover ES6 functional features such as spread operators and generators

Who This Book Is For:

JavaScript developers (or beginners) who want to understand functional programming concepts and the functional nature of the language.

Anto Aravinth [Anto Aravinth]: author's other books


Who wrote Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6? 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: Functional Programming with JavaScript Using EcmaScript 6 — 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: Functional Programming with JavaScript Using EcmaScript 6" 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 2017

Anto Aravinth , Beginning Functional JavaScript , 10.1007/978-1-4842-2656-8_10

10. Pause, Resume with Generators

Anto Aravinth 1

(1) Chennai, Tamil Nadu, India

Note

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

Once checkout the code, please checkout branch chap10 :

...

git checkout -b chap10 origin/chap10

...

For running the codes, as before run:

...

npm run playground

...

We started the book with a simple definition of functions. Then we constantly saw how to use functions to do great things using 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 still we have not talked about yet another important technique that every JavaScript developer should be aware of asynchronous code.

You have dealt with a whole lot 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 I'm going to showcase here is using ES6 Generators. Generators are new specs for functions in ES6. Generators are not really a functional programming technique; however, its part of a function (functional programming is about function, right?); for that reason we have dedicated a chapter for it in this functional programming book!

Even if you are a big fan of promises (which is a technique for solving the callback problem), I would still advise you to have a look at this chapter. I will bet you, that you are going to love generators and the way they solve the async code problems!

Async Code and Its Problem

Before we really see what generators are, let's discuss the problem of handling async code in JavaScript in this section. We are going to talk about a Callback Hell problem . If you already knew what it is, you are free to move to the next section. For others, please read on.

Callback Hell

Imagine you have a function like the following:

Listing 10-1. Synchronous functions
let sync = () => {
//some operation
//return data
}
let sync2 = () => {
//some operation
//return data
}
let sync3 = () => {
//some operation
//return data
}

The above 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:

Listing 10-2. Asynchronous functions
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 */)
}

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 like this:

Listing 10-3. Async functions calling example
async(function(x){
async2(function(y){
async3(function(z){
...
});
});
});

Oops! You can see in the above code (Listing ), 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 the above problem. Promises are great, but given the fact that ES6 has generators at language level, we dont need Promises anymore!

Generators 101

As mentioned, generators are part of ES6 specifications and its bundled up at language level. We talked about using generators helping in handling async code . But before we get there, we are going to talk about the fundamentals of generators. This section will focus on explaining the core concepts behind generators. Once we learn the basics, we will be creating a generic function using generators to handle async code in our library. Thats the plan of this chapter. 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 the following:

Listing 10-4. First Simple Generator
function* gen() {
return 'first generator';
}

The function gen in the above code is a generator. As you might notice, we have used an asterisk before our function name (in this case gen ) to denote that its a generator function! Al right, 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 first generator value? Lets print it on the console and inspect it:

console.log(generatorResult)

The result will be:

gen {[[GeneratorStatus]]: "suspended", [[GeneratorReceiver]]: Window}

The above result shows that the generatorResult is not a normal function, but an instance of Generator primitive type! So the question is how to get the value from this generator instance? The answer is to call the function next , which is available on the generator instance. So to get the value you need to do this:

generator.next()

The above code returns:

Object {value: "hello world", done: true}

As you can see the returned object from next has value and is done . So we need to call next along with fetching a value from the object:

generator.next().value
=> 'first generator'

Thats great!

Caveats of Generators

The above examples show how to create a generator, how to create an instance for it, and how it gets value. But there are a few important things we need to take care of while we are working with generators.

The first thing is that we can't 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 Listing for 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 the above 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 can't consume it again. In our case generatorResult is a sequence that has value as a first generator . With our first call to next , we (as the caller of the generator) have consumed the value from the sequence. Since the sequence is empty by now, calling it a second time will return you undefined !

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6»

Look at similar books to Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6. 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: Functional Programming with JavaScript Using EcmaScript 6»

Discussion, reviews of the book Beginning Functional JavaScript: Functional Programming with JavaScript Using EcmaScript 6 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.