Helping creating equivalent future::value() from package promises

Good afternoon!

I'm building a shiny app that will be integrating the future and promises package. I have a unique need when the user saves their work, the end results of the promises will need to be saved to a database. This will require me to extract the results of the promise.

I was able to create the following functions to interact with promises. I am able to get the state of the promise, and the end value with get_promise_state() and get_promise_value(). The problem is, if at the time of running get_promise_value() the promises aren't fulfilled, the function will return NULL. I tried a while statement to wait until the promise is fulfilled, but it doesn't seem to be working. I think that when the promise is passed to get_promise_value_wait() the state of the promise at that moment is getting passed. I was hoping this function would work similarily to future::value()

Does anyone have any suggestions?


library(tidyverse)
library(promises)
library(future)
plan(multisession)

get_promise_state <- function(x){
  environment(x[['then']])[['private']][['state']]
}

is_promise_resolved <- function(x){
  get_promise_state(x) == 'fulfilled'
}

get_promise_value <- function(x){
  environment(x[['then']])[['private']][['value']]
}

get_promise_value_wait <- function(x) {

  
  while (!is_promise_resolved(x)) {
    state <- get_promise_state(x)
    message(state)
    if (state == 'rejected') stop('Promise Rejected')
    Sys.sleep(1)
  }
  
  get_promise_value(x)
  
  
}

p1 <- future({
  Sys.sleep(5)
  mtcars
})

p2 <- p1 %...>% filter(cyl == '6')

get_promise_state(p2)

get_promise_value(p2)

is_promise_resolved(p2)

get_promise_value_wait(p2)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.