Skip to content

Commit

Permalink
🐛 Explicitly handle yieldTo promise resolution and rejection (#1017)
Browse files Browse the repository at this point in the history
If we allow yieldTo promise errors to bubble, some version of node throws an error. These type of
errors can be avoided completely by always explicitly handling promise resolution and rejection.
  • Loading branch information
wwilsman authored Jul 29, 2022
1 parent 0b85c22 commit 235b327
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,19 @@ export class AbortError extends Error {

// An async generator that yields after every event loop until the promise settles
export async function* yieldTo(subject) {
let pending = typeof subject?.finally === 'function';
if (pending) subject.then(() => (pending = false), () => (pending = false));
// yield to any provided generator or return non-promise values
if (isGenerator(subject)) return yield* subject;
if (typeof subject?.then !== 'function') return subject;

// update local variables with the provided promise
let result, error, pending = !!subject
.then(r => (result = r), e => (error = e))
.finally(() => (pending = false));

/* eslint-disable-next-line no-unmodified-loop-condition */
while (pending) yield new Promise(r => setImmediate(r));
return isGenerator(subject) ? yield* subject : await subject;
if (error) throw error;
return result;
}

// An async generator that runs provided generators concurrently
Expand Down

0 comments on commit 235b327

Please sign in to comment.