GENERATORS
Generators are a delightfully flexible construct introduced in the ECMAScript 6 specification that offers the ability to pause and resume code execution inside a single function block. The implications of this new ability are profound; it allows for, among many other things, the ability to define custom iterators and implement coroutines.
Generator Basics
Generators take the form of a function, and the generator designation is performed with an asterisk. Anywhere a function definition is valid, a generator function definition is also valid:
// Generator function declaration
function* generatorFn() {}
// Generator function expression
let generatorFn = function* () {}
// Object literal method generator function
let foo = {
* generatorFn() {}
}
// Class instance method generator function
class Foo {
* generatorFn() {}
}
// Class static method generator function
class Bar {
static * generatorFn() {}
}