Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active April 29, 2020 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfkaye/2efecbef8af9e2c6b512fa1ee374543e to your computer and use it in GitHub Desktop.
Save dfkaye/2efecbef8af9e2c6b512fa1ee374543e to your computer and use it in GitHub Desktop.
Notes from Eric Rey (2018), "Iterators and Generators do have a place in modern JavaScript"
// 17 Oct 2019
// Notes from Eric Rey, "Iterators and Generators do have a place in modern JavaScript" (25 Aug 2018)
// https://itnext.io/iterators-and-generators-do-have-a-place-in-modern-javascript-d4cb589b491
// I LIKE THE FIRST USE
// Custom Collections
function EvenNumbers(values = []) {
values = [...values];
return {
addElement(value) {
values.push(value)
},
*[Symbol.iterator]() {
for (value of values) {
if (value % 2 == 0) {
yield value;
}
}
}
}
}
var collection = EvenNumbers([1,2,3]);
collection.addElement(12);
var even = [...collection];
console.log(even);
// I DON'T LIKE THE NEXT EXAMPLE
// Async code that looks synchronous
// fetchData()
// there's something deeply wrong there I can't put my finger on it yet...
// 28 April 2020: figured it out
// see gist, async-generator-example.js -> https://gist.github.com/dfkaye/83bd9455a9daa5c561d9295a80856bde
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment