Skip to content

Commit

Permalink
🐛 Avoid recursion in generators due to no tail-call optimizations (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Wilsman authored Jan 24, 2022
1 parent 5f0279d commit 2e678a7
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ export function waitFor(predicate, options) {
? { timeout: options } : (options || {});

return generatePromise(async function* check(start, done) {
if (timeout && Date.now() - start >= timeout) {
throw new Error(`Timeout of ${timeout}ms exceeded.`);
} else if (!predicate()) {
yield new Promise(r => setTimeout(r, poll));
return yield* check(start);
} else if (idle && !done) {
yield new Promise(r => setTimeout(r, idle));
return yield* check(start, true);
while (true) {
if (timeout && Date.now() - start >= timeout) {
throw new Error(`Timeout of ${timeout}ms exceeded.`);
} else if (!predicate()) {
yield new Promise(r => setTimeout(r, poll, (done = false)));
} else if (idle && !done) {
yield new Promise(r => setTimeout(r, idle, (done = true)));
} else {
return;
}
}
}(Date.now()));
}

0 comments on commit 2e678a7

Please sign in to comment.