Wednesday, March 29, 2017

ES6: Generators

Generators

It is a function that you can start and stop, play and pause

Put an asterisk after function 

Uses yield keyword

function* listPeople(){
yield ‘AK’;
yield ‘BK’;
yield ‘CK’;
}

const people = listPeople();
-Returns a generator

Call .next() on it
Returns an Object

people.next();
-Returns AK object
people.next();
-Returns BK object

The generator has a ‘done’ property
After all yields are done, it gets set to true.


No comments:

Post a Comment