Skip to content

Instantly share code, notes, and snippets.

@tomcask
Created May 30, 2017 09:38
Show Gist options
  • Save tomcask/a59353f592b239a6e6a237099e355518 to your computer and use it in GitHub Desktop.
Save tomcask/a59353f592b239a6e6a237099e355518 to your computer and use it in GitHub Desktop.

Como trabajan los iteradores

class RangeIterator {
  constructor(start, stop) {
    this.value = start;
    this.stop = stop;
  }
 
  [Symbol.iterator]() { return this; }
 
  next() {
    var value = this.value;
    if (value < this.stop) {
      this.value++;
      return {done: false, value: value};
    } else {
      return {done: true, value: undefined};
    }
  }
}

// with generators
function* range(start, stop) {
  for (var i = start; i < stop; i++)
    yield i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment