async_generator {coro} | R Documentation |
Construct an async generator
Description
An async generator constructs iterable functions that are also
awaitables. They support both the yield()
and await()
syntax.
An async iterator can be looped within async functions and
iterators using await_each()
on the input of a for
loop.
The iteration protocol is derived from the one described in
iterator
. An async iterator always returns a
promise. When the iterator is exhausted, it returns a resolved
promise to the exhaustion sentinel.
Usage
async_generator(fn)
await_each(x)
Arguments
fn |
An anonymous function describing an async generator
within which |
x |
An awaitable value, i.e. a promise. |
Value
A generator factory. Generators constructed with this
factory always return promises::promise()
.
See Also
async()
for creating awaitable functions;
async_collect()
for collecting the values of an async iterator;
coro_debug()
for step-debugging.
Examples
# Creates awaitable functions that transform their inputs into a stream
generate_stream <- async_generator(function(x) for (elt in x) yield(elt))
# Maps a function to a stream
async_map <- async_generator(function(.i, .fn, ...) {
for (elt in await_each(.i)) {
yield(.fn(elt, ...))
}
})
# Example usage:
if (interactive()) {
library(magrittr)
generate_stream(1:3) %>% async_map(`*`, 2) %>% async_collect()
}