rstudio / promises

A promise library for R
https://rstudio.github.io/promises
Other
197 stars 19 forks source link

Support `await` syntax #83

Closed lz100 closed 1 year ago

lz100 commented 1 year ago

Is there a way to wait for the promises to complete?

Something like

library(promises)
library(future)
plan(multisession)
promise_map(1:3, function(x) {
    future_promise({
        Sys.sleep(5)
        Sys.time()
    })
}) %...>% await()

and maybe timeout also, ... %...>% await(timeout = 10, rejected = {...})

As for now, it directly returns me the prompt, and does not wait. If I have some other expressions that need the value from this promise, it will fail.

jcheng5 commented 1 year ago

Are you sure you want the promised package, and not the future package by itself? The entire point of the promises package is not to wait; if you want to wait, then future should work well, no?

lz100 commented 1 year ago

That's true, but I can do "not to wait" with some simple wrappers with future too, then the point of promises package seems less meaningful. The description of the packages says "Asynchronous programming is a technique used by many programming languages". Then in "many programming languages", the await or similar syntax is supported, like Javascript, Python, C++, Golang, etc. Why in R do we need to use two packages (double learning curve) to achieve what should be in the same system? I would feel it is bad not to have any blocking mechanism in an async library.

jcheng5 commented 1 year ago

If you're working at the console then the promises package is almost certainly going to be useless for you. It's designed for Shiny and Plumber to interleave operations belonging to different sessions/requests on a single R thread. The reason it exists is to let you chain together sequences of operations that have one or more asynchronous components. If none of this applies to what you're doing, you should absolutely just use future.