Skip to content

Commit 7747baf

Browse files
committed
Break the while loop early in touchNext
1 parent dca60f8 commit 7747baf

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/cached_async_iterable.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ export default class CachedAsyncIterable extends CachedIterable {
7676
async touchNext(count = 1) {
7777
let idx = 0;
7878
while (idx++ < count) {
79-
if (this.length === 0 || this[this.length - 1].done === false) {
80-
this.push(await this.iterator.next());
79+
const last = this[this.length - 1];
80+
if (last && last.done) {
81+
break;
8182
}
83+
this.push(await this.iterator.next());
8284
}
8385
// Return the last cached {value, done} object to allow the calling
8486
// code to decide if it needs to call touchNext again.

src/cached_sync_iterable.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ export default class CachedSyncIterable extends CachedIterable {
4646
touchNext(count = 1) {
4747
let idx = 0;
4848
while (idx++ < count) {
49-
if (this.length === 0 || this[this.length - 1].done === false) {
50-
this.push(this.iterator.next());
49+
const last = this[this.length - 1];
50+
if (last && last.done) {
51+
break;
5152
}
53+
this.push(this.iterator.next());
5254
}
5355
// Return the last cached {value, done} object to allow the calling
5456
// code to decide if it needs to call touchNext again.

0 commit comments

Comments
 (0)