I'm tackling Generators in ES6, and I would like to understand, conceptually, what's happening in the function below:
function* createNames() {
const people = [];
people.push(yield);
people.push(yield);
people.push(yield);
return people;
}
const iterator = createNames();
iterator.next('Brian');
iterator.next('Paul');
iterator.next('John');
iterator.next(); // output: ["Paul", "John", undefined]
My question is: why does the first push is ignored? Shouldn't the array be something like people = ['Brian', 'John', 'Paul', undefined]
? Sorry for the silly question, but I would really love to be able to fully grasp this. Thanks in advance!