class JS::PromiseScheduler

Public Class Methods

new(loop) click to toggle source
# File packages/gems/js/lib/js.rb, line 74
def initialize(loop)
  @loop = loop
end

Public Instance Methods

await(promise) click to toggle source
# File packages/gems/js/lib/js.rb, line 78
def await(promise)
  current = Fiber.current
  promise.call(
    :then,
    ->(value) { current.transfer(value, :success); nil },
    ->(value) { current.transfer(value, :failure); nil }
  )
  if @loop == current
    raise (
            "JS::Object#await can be called only from RubyVM#evalAsync or RbValue#callAsync JS API\n" +
              "If you are using browser.script.iife.js, please ensure that you specify `data-eval=\"async\"` in your script tag\n" +
              "e.g. <script type=\"text/ruby\" data-eval=\"async\">puts :hello</script>\n" +
              "Or <script type=\"text/ruby\" data-eval=\"async\" src=\"path/to/script.rb\"></script>"
          )
  end
  value, status = @loop.transfer
  raise JS::Error.new(value) if status == :failure
  value
end