tests/testthat/helper-sync.R

# Given a promise-yielding expression, loop until it resolves or rejects.
# DON'T USE THIS TECHNIQUE IN SHINY, PLUMBER, OR HTTPUV CONTEXTS.
sync <- function(expr) {
  p <- force(expr)

  done <- FALSE
  success <- NULL
  error <- NULL

  promises::then(
    p,
    function(result) {
      success <<- result
      done <<- TRUE
    },
    function(err) {
      error <<- err
      done <<- TRUE
    }
  )

  while (!done) {
    later::run_now(0.25)
  }
  if (!is.null(error)) {
    stop(error)
  } else {
    success
  }
}

expect_promise <- function(p, state = NULL) {
  name <- deparse(substitute(p))
  expect(
    promises::is.promise(p),
    sprintf("`%s` is not a promise", name)
  )
  if (!is.null(state)) {
    expect_equal(attr(p, "promise_impl")$status(), state)
  }
}

Try the shinychat package in your browser

Any scripts or data that you put into this service are public.

shinychat documentation built on June 10, 2025, 9:09 a.m.