Skip to content

Instantly share code, notes, and snippets.

@zbraniecki
Last active March 16, 2017 19:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbraniecki/56279c9e14b6ab464224682c8a59a8f0 to your computer and use it in GitHub Desktop.
Save zbraniecki/56279c9e14b6ab464224682c8a59a8f0 to your computer and use it in GitHub Desktop.
[JS] Iterable that can be replayed and caches resolved results
function cachedIterable(iterable) {
const cache = [];
return {
[Symbol.iterator]() {
return {
ptr: 0,
next() {
if (cache.length <= this.ptr) {
cache.push(iterable.next());
}
return cache[this.ptr++];
}
};
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment