Skip to content

Instantly share code, notes, and snippets.

@mstade
Last active August 29, 2015 13:58
Show Gist options
  • Save mstade/10012933 to your computer and use it in GitHub Desktop.
Save mstade/10012933 to your computer and use it in GitHub Desktop.
ES6 auto-rewinding iterators
function range(n) {
var i = 0
return { next: step }
function step() {
if (i++ < n) return { value: i }
return i = 0, { done: '110%' }
}
}
var ten = range(5)
for (var n of ten) console.log(n)
console.log('erase and rewind')
for (var n of ten) console.log(n) // works
@mstade
Copy link
Author

mstade commented Apr 7, 2014

$ node --harmony auto-rewind-it.js 
1
2
3
4
5
erase and rewind
1
2
3
4
5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment