Create an async function, that returns a deferred value, from a regular function. If fun is already an async function, then it does nothing, just returns it.

async(fun)

Arguments

fun

Original function.

Value

Async version of the original function.

Details

The result function will have the same arguments, with the same default values, and the same environment as the original input function.

Examples

f <- function(x) 42
af <- async(f)
is_async(f)
#> [1] FALSE
is_async(af)
#> [1] TRUE
f()
#> [1] 42
synchronise(dx <- af())
#> [1] 42
dx
#> <deferred>
#>   Public:
#>     cancel: function (reason = "Cancelled") 
#>     catch: function (...) 
#>     clone: function (deep = FALSE) 
#>     event_emitter: NULL
#>     finally: function (on_finally) 
#>     initialize: function (action = NULL, on_progress = NULL, on_cancel = NULL, 
#>     share: function () 
#>     then: function (on_fulfilled) 
#>   Private:
#>     action: NULL
#>     add_as_parent: function (child) 
#>     cancel_callback: NULL
#>     cancelled: FALSE
#>     children: list
#>     dead_end: FALSE
#>     event_loop: event_loop, R6
#>     get_info: function () 
#>     id: 2
#>     make_error_object: function (err) 
#>     maybe_cancel_parents: function (reason) 
#>     mycall: call
#>     null: function () 
#>     parent_reject: function (value, resolve) 
#>     parent_resolve: function (value, resolve) 
#>     parents: NULL
#>     progress: function (data) 
#>     progress_callback: NULL
#>     reject: function (reason) 
#>     resolve: function (value) 
#>     run_action: function () 
#>     running: TRUE
#>     shared: FALSE
#>     state: fulfilled
#>     type: async
#>     update_parent: function (old, new) 
#>     value: 42