question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion needed - Wrapping AsyncGenerator

See original GitHub issue

Sorry 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:open
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
konradekkcommented, Nov 6, 2022

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.

1reaction
supermacrocommented, Jul 21, 2021

I’m adding a bounty to this if it ever gets resolved; $100 USD

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found