Javascript – ES6 Generators

console.clear();

//--------------------
// Infinite Generator
//--------------------

function* sequence1(start) {
  while (true) {
    yield start++
  }
}

var s1 = sequence1(5);

console.log("Infinite Generator");
console.log(s1.next()); // <- {value: 5, done: false}
console.log(s1.next()); // <- {value: 6, done: false}
console.log(s1.next()); // <- {value: 7, done: false}
console.log(s1.next()); // <- {value: 8, done: false}


//------------------
// Finite Generator
//------------------

function* sequence2(start) {
  while (start < 7) {
    yield start++
  }
  return 0;
}

var s2 = sequence2(5);

console.log("Finite Generator");
console.log(s2.next()); // <- {value: 5, done: false}
console.log(s2.next()); // <- {value: 6, done: false}
console.log(s2.next()); // <- {value: 0, done: true}
console.log(s2.next()); // <- {value: undefined, done: true}


//------------------
// Next() Arguments
//------------------

function* sequence3(start) {
  while (true) {
    var newStart = yield start++;
    if (newStart) {
      start = newStart;
    }
  }
}

var s3 = sequence3(5);

console.log("Next() Arguments");
console.log(s3.next(2)); // <- {value: 5, done: false}
console.log(s3.next()); // <- {value: 6, done: false}
console.log(s3.next(2)); // <- {value: 2, done: false}
console.log(s3.next()); // <- {value: 3, done: false}

/*
When the execution reach the yield instruction the value is returned and the execution is frozen right before associating the value returned by the yield itself. It will restart from there at the next call to next().

- The 1st call to next() will execute until the yield (so it will return 5). We can look at the 1st call as a sort of initialisation.

- The 2nd call to next() will execute from where the previous execution left, so will check newStart, that in the meatine has been populated with the 2nd call's argument (so undefined), and will loop until the yield again (so it will return 6).

- The 3rd call to next() will execute from where the previous execution left, so will check newStart, that in the meatine has been populated with the 3rd call's argument (so 2), and will loop until the yield again (so it will return 2).
*/


//--------------------
// Awaiting for async
//--------------------

function sumAsync(val1, val2) {
  setTimeout(function() {
    iterator.next(val1 + val2);
  }, 1000);
}

function* generator() {
  console.log("Awaiting for async");
  console.log(yield sumAsync(2, 3));
  console.log(yield sumAsync(3, 4));
  console.log(yield sumAsync(4, 5));
  console.log(yield sumAsync(5, 6));
}

var iterator = generator();
iterator.next();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s