async_collect()
takes an asynchronous iterator, i.e. an iterable
function that is also awaitable. async_collect()
returns an
awaitable that eventually resolves to a list containing the values
returned by the iterator. The values are collected until exhaustion
unless n
is supplied. The collection is grown geometrically for
performance.
Examples
# Emulate an async stream by yielding promises that resolve to the
# elements of the input vector
generate_stream <- async_generator(function(x) for (elt in x) yield(elt))
# You can await `async_collect()` in an async function. Once the
# list of values is resolved, the async function resumes.
async(function() {
stream <- generate_stream(1:3)
values <- await(async_collect(stream))
values
})
#> <async>
#> function ()
#> {
#> stream <- generate_stream(1:3)
#> values <- await(async_collect(stream))
#> values
#> }
#> <environment: 0x55d326a942a0>