fix(func): await Promises returned from async generators #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #17 - Returning a Promise from an async generator in
funcwraps the Promise instead of awaiting it.Problem
When using
funcwith an async generator (async function*) and returning a Promise at the end (e.g., from another function call), the returned value becameResult<Promise<T>>instead ofResult<T>. The Promise was not awaited before being wrapped in the Result.Root Cause
In the
handleIterationfunction withinResult.func, when an async generator completed (iteration.done === true), the code checked if the value was aResultinstance. Wheniteration.valuewas aPromise, it passed theinstanceof Resultcheck asfalse, so it got wrapped inResult.ok(Promise)instead of being awaited first.Solution
Modified the async generator iteration path to check if the final value is a
Result.Okcontaining a Promise. When detected:Result.ok()toThrownErrorChanges
src/result.ts: Added Promise detection and awaiting logic in the async generator completion handlertests/issues/#17-async-generator-promise-wrap.test.ts: Minimal reproduction test for issue fix:func(async)infinite recursion in function execution #8tests/promises.test.ts: Comprehensive test suite covering Promise handling in async generatorsTest Results
Workaround (no longer needed)
Previously, users had to use
yield* awaitinstead of directly returning the Promise: