Skip to content

Commit

Permalink
Properly distinguish sync and async iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Oct 23, 2023
1 parent 4d32eba commit aa4b8ca
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions util/RangeIterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class RangeIterable {
let source = this;
let iterable = new RangeIterable();
iterable.iterate = (async) => {
let iterator = source[Symbol.iterator](async);
let iterator = source[async ? Symbol.asyncIterator : Symbol.iterator]();
let i = 0;
return {
next(resolvedResult) {
Expand All @@ -30,6 +30,7 @@ export class RangeIterable {
} else {
iteratorResult = iterator.next();
if (iteratorResult.then) {
if (!async) throw new Error('Can synchronously iterate with asynchronous values');
return iteratorResult.then(iteratorResult => this.next(iteratorResult));
}
}
Expand All @@ -40,6 +41,7 @@ export class RangeIterable {
}
result = func(iteratorResult.value, i++);
if (result && result.then) {
if (!async) throw new Error('Can synchronously iterate with asynchronous values');
return result.then(result =>
result === SKIP ?
this.next() :
Expand Down Expand Up @@ -69,7 +71,7 @@ export class RangeIterable {
return iterable;
}
[Symbol.asyncIterator]() {
return this.iterator = this.iterate();
return this.iterator = this.iterate(true);
}
[Symbol.iterator]() {
return this.iterator = this.iterate();
Expand Down Expand Up @@ -101,11 +103,12 @@ export class RangeIterable {
iterator = secondIterable[Symbol.iterator](async);
result = iterator.next();
if (concatIterable.onDone) {
if (result.then)
if (result.then) {
if (!async) throw new Error('Can synchronously iterate with asynchronous values');
result.then((result) => {
if (result.done()) concatIterable.onDone();
});
else if (result.done) concatIterable.onDone();
} else if (result.done) concatIterable.onDone();
}
} else {
if (concatIterable.onDone) concatIterable.onDone();
Expand All @@ -115,11 +118,13 @@ export class RangeIterable {
return {
next() {
let result = iterator.next();
if (result.then)
if (result.then) {
if (!async) throw new Error('Can synchronously iterate with asynchronous values');
return result.then((result) => {
if (result.done) return iteratorDone(result);
return result;
});
}
if (result.done) return iteratorDone(result);
return result;
},
Expand Down

0 comments on commit aa4b8ca

Please sign in to comment.