OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Is there a way to get the return value of an async js generator function without a while loop

  • Thread starter Thread starter Nick Gazzillo
  • Start date Start date
N

Nick Gazzillo

Guest
I have an async generator function that I would like to just get the return value for without calling next a bunch of times. Right now I have something like this:

Code:
async function *getData(): AsyncGenerator<object[], object[]>
{
    let results = []
    let res = {};

    while (true)
    {
        res = await apiCall(res.next);
        results.push(...res.data)
        if (!res.next) break;
        yield res.data;
    }
    return results;
}

async function main()
{
    const genFn = getData();
    while (!(await genFn.next()).done) {}
}

Is there a better/more concise way that doesn't need the while loop?

<p>I have an async generator function that I would like to just get the return value for without calling next a bunch of times. Right now I have something like this:</p>
<pre><code>async function *getData(): AsyncGenerator<object[], object[]>
{
let results = []
let res = {};

while (true)
{
res = await apiCall(res.next);
results.push(...res.data)
if (!res.next) break;
yield res.data;
}
return results;
}

async function main()
{
const genFn = getData();
while (!(await genFn.next()).done) {}
}
</code></pre>
<p>Is there a better/more concise way that doesn't need the while loop?</p>
 

Latest posts

Top