Suggestion needed - Wrapping AsyncGenerator
See original GitHub issueSorry for opening an issue with what is basically a question/ feedback for API ergonomics…
Has any of you stumbled upon a situation where you’d want to wrap an AsynGenerator in a Result somehow where the ultimate goal is to typify the possible errors and effectively not have it throwing exceptions?
Let’s say that the AsyncGenerator is wrapping a REST API Call and yielding results over time:
type SomeData = {};
type SomeApiResult = {
content: SomeData[];
nextToken?: string;
};
const getData = async (nextToken?: string): Promise<SomeApiResult> => /*...*/;
async function* getAllData(): AsyncGenerator<SomeData> {
let loadMore = true;
let prevNextToken: string | undefined = undefined;
while (loadMore) {
const result = await getData(prevNextToken);
for (const data of result.content) {
yield data;
}
loadMore = result.nextToken !== prevNextToken;
prevNextToken = result.nextToken;
}
}
One way I see it, is to have the generator return Results and as soon as an exception is thrown/promise rejection occurs, yield the error. This breaks the iteration as soon as the error occurs however, the side effect is that now the consumer has to unwrap each received value from the result and this breaks the norms of common generators.
async function* getAllData(): AsyncGenerator<Result<SomeData, SomeError>>;
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Async iteration and generators
Asynchronous iteration is needed when values come asynchronously: after ... In an async generator, we should add await , like this:.
Read more >Looking for batching approach for JS Async generator
I am trying to add batching capability to an async js generator. The idea is to have a function that would wrap around...
Read more >Async/Generator functions in CLJS - (requesting feedback!)
I've been drafting a proposal for using JavaScript's parking primitives (e.g. yield, await) in ClojureScript, and it's ready for more ...
Read more >Use code generators to transform a .NET codebase ...
The AsyncGenerator will just call these synchronous APIs as needed and wrap the calls in e.g. a Task.FromResult invocation when needed as shown...
Read more >async_generator 1.8
Fix a subtle bug where if you wrapped an async generator using functools.wraps, ... It's probably fixable, but needs some fiddling with ctypes...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That case would be better solved by streams (see e.g. RxJS solutions). What you roughly do here is: maintain a stream reading iterables that you flatten (with mapping) into the stream. You could convert to async iteration from the streaming with some small effort (see e.g. this implementation on StackOverflow).
Iʼd second @paduc here (providing him with bounty!) and avoid
Result
completely here.I’m adding a bounty to this if it ever gets resolved; $100 USD