{mirai} supplies its own as.promise()
method, allowing
it to be used as a promise from the promises
package.
A ‘mirai’ may be piped directly using the promise pipe
&...>%
, which implicitly calls
as.promise()
on the ‘mirai’.
Alternatively, it may be converted into a promise by
as.promise()
, which then allows using the methods
$then()
, $finally()
etc.
The following example outputs “hello” to the console after one second when the ‘mirai’ resolves.
library(mirai)
library(promises)
p <- mirai({Sys.sleep(1); "hello"}) %...>% cat()
p
#> <Promise [pending]>
It is possible to both access a ‘mirai’ value at $data
and to use a promise for enacting a side effect (assigning the value to
an environment in the example below).
library(mirai)
library(promises)
env <- new.env()
m <- mirai({Sys.sleep(1); "hello"})
p <- as.promise(m)$then(function(x) env$res <- x)
call_mirai(m)$data
#> [1] "hello"
# after returning to the top level prompt:
env$res
#> [1] "hello"
Used in combination with promises
>= 1.3.0,
is.promising()
returns TRUE. This allows ‘mirai’ to be used
seamlessly in places accepting promises, such as Shiny ExtendedTasks,
without the need to explicitly convert using
as.promise()
.
is.promising(m)
#> [1] TRUE