async_generator | R Documentation |
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.
async_generator(fn)
await_each(x)
fn |
An anonymous function describing an async generator
within which |
x |
An awaitable value, i.e. a promise. |
A generator factory. Generators constructed with this
factory always return promises::promise()
.
async()
for creating awaitable functions;
async_collect()
for collecting the values of an async iterator;
coro_debug()
for step-debugging.
# 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()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.