Generator in ES6 and solving callback hell
1. What is Generator
?
Generally, Generator
is a object returned by a generator function that behaves like an Iterator
. In Javascript, according to Mozilla:
The Generator object is returned by a generator function and it conforms to both the iterator and the Iterable protocol.
While a normal function will return a value using return
keyword, Generator
uses yield
keyword to generate a rule to create values rather than the actual values. In other words, it is the lazily way to generate values.
The obvious benefit of Generator
is improving the performance and help us to organize source code better. This is a new feature in ES6, but it has been implemented in other languages (e.g.Python, C# etc) for a long time.
2. Syntax
To define a generator function we can use GeneratorFunction
constructor and function* expression
name
The function name.param
The name of an argument to be passed to the function. A function can have up to 255 arguments.statements
The statements comprising the body of the function.
Futhermore, we can also using [yield*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*)
to yield another generator, and note that:
The next() method also accepts a value which can be used to modify the internal state of the generator. A value passed to next() will be treated as the result of the last yield expression that paused the generator.
3. Generator
usage
3.1 Simple example
In this simple example, instead of creating an array to store all odd numbers, we just simply create a rule
for generating them.
3.2 What is callback hell
and how to solve it?
First, let’s take a look at this callback hell or pyramid of doom
How can we make it better? Using Promise
is an answer. Obviously, the code looks more tidy and easy to read.
Promise
(technically in this case: jQuery Promise
) is a good way to prevent callback hell. But you know what? Generator
is more awesome. This example below uses bluebird Promise.coroutine
to return a function that can use yield
to yield promise.
4. Conlusion
Generator
is a new feature in ECMAScript 6 and it is a convenient way to control iteration behavior of a loop. Moreover, it also help to solve the callback hell in your existing code. Combine Promise
and Generator
effectively can help you control the asynchronous flow better and preventing callback hell in your code. It is more powerful and useful than just only make your code more concise and tidy. Using it wisely can aslo help you to improve your code performance compare to tradditional loop
.
Full code is here: http://plnkr.co/edit/DFowxQiDYCmxzhm3RvVB?p=info