Javascript generator - iterator - or did i completed miss the idea out.


Nice .... good to know javascript supports iterator with a function * (asterisk).

let index;

// declares our iterator function which returns number from i to 9

const generatorFunction = function* ()
{
    for (var i=0; i < 10; i++)
    {
        yield i;
    }
};

////////////////////////////////////////////////////////////////////v
// logs out output
////////////////////////////////////////////////////////////////////v

for (index of generatorFunction ()) {
    console.log(index);
}

output will be something like

0
1
2
3
4
5
6
7
8
9

Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm