Skip to content

Instantly share code, notes, and snippets.

@mstade
Last active August 29, 2015 13:58
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 mstade/10012898 to your computer and use it in GitHub Desktop.
Save mstade/10012898 to your computer and use it in GitHub Desktop.
ES6 rewindable iterators
function range(n) {
var i = 0
return { next: step, rewind: rewind }
function step() {
return i++ < n? { value: i } : { done: '110%' }
}
function rewind() { i = 0 }
}
var ten = range(5)
for (var n of ten) console.log(n)
for (var n of ten) console.log('not called')
ten.rewind()
console.log('erase and rewind')
for (var n of ten) console.log(n) // Works again
@mstade
Copy link
Author

mstade commented Apr 7, 2014

$ node --harmony rewindable-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