Skip to content

Commit

Permalink
Break the while loop early in touchNext
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jun 8, 2018
1 parent dca60f8 commit 7747baf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/cached_async_iterable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ export default class CachedAsyncIterable extends CachedIterable {
async touchNext(count = 1) {
let idx = 0;
while (idx++ < count) {
if (this.length === 0 || this[this.length - 1].done === false) {
this.push(await this.iterator.next());
const last = this[this.length - 1];
if (last && last.done) {
break;
}
this.push(await this.iterator.next());
}
// Return the last cached {value, done} object to allow the calling
// code to decide if it needs to call touchNext again.
Expand Down
6 changes: 4 additions & 2 deletions src/cached_sync_iterable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ export default class CachedSyncIterable extends CachedIterable {
touchNext(count = 1) {
let idx = 0;
while (idx++ < count) {
if (this.length === 0 || this[this.length - 1].done === false) {
this.push(this.iterator.next());
const last = this[this.length - 1];
if (last && last.done) {
break;
}
this.push(this.iterator.next());
}
// Return the last cached {value, done} object to allow the calling
// code to decide if it needs to call touchNext again.
Expand Down

0 comments on commit 7747baf

Please sign in to comment.