tests/testthat/test-cpp.R

describe("C++ interface", {
  it("basically works", {
    promise(function(resolve, reject) {
      asyncFib(resolve, reject, 3)
    }) %...>%
      { expect_identical(., 2) } %>%
      wait_for_it()
  })

  it("works with domains", {
    cd <- create_counting_domain()

    expect_true(is.null(current_promise_domain()))
    with_promise_domain(cd, {
      promise(function(resolve, reject) {
        asyncFib(resolve, reject, 3)
      }) %...>%
        {
          expect_identical(., 2)
          expect_identical(cd$counts$onFulfilledCalled, 1L)
          promise_resolve(TRUE) %...>% {
            expect_true(!is.null(current_promise_domain()))
            expect_identical(cd$counts$onFulfilledCalled, 2L)
          }
        } %>%
        wait_for_it()
    })
  })
})

Try the promises package in your browser

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

promises documentation built on April 4, 2025, 3:07 a.m.