Why is the return value of a function with a multisession future different in plumber?

31 Views Asked by At

When comparing the first two code snippets below it is apparent that the do.call() setup will not return 4 but rather an object from the futureverse. On the other hand the plumber setup seems to resolve this "automatically" and returns the 4 in the "Response body" of the API.

I'm aware that return values can be accessed via future::value() (see chunk three) but I don't understand why this seems to work automatically in plumber. Can someone clarify this?

Here is the code to run the function in plumber context:

# put this code into the file plumber.R and run:
# plumber::plumb("plumber.R")$run(port=9999)
# click "Try it out" and then "Execute"
# the Response body shows the 4
future::plan(future::multisession)

#* @get /async_sqrt
function() {
  future::future({4})
}

Here is the code to run the function via do.call().

do.call(function() {
  future::future({4})
}, args = list())

To retrieve the 4 in do.call() context this needs to be adapted as so:

do.call(function() {
  future::value(future::future({4}))
}, args = list())
0

There are 0 best solutions below