Class: JS::PromiseScheduler

Inherits:
Object
  • Object
show all
Defined in:
ext/js/lib/js.rb

Instance Method Summary collapse

Constructor Details

#initialize(loop) ⇒ PromiseScheduler

Returns a new instance of PromiseScheduler.



74
75
76
# File 'ext/js/lib/js.rb', line 74

def initialize(loop)
  @loop = loop
end

Instance Method Details

#await(promise) ⇒ Object

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'ext/js/lib/js.rb', line 78

def await(promise)
  current = Fiber.current
  promise.call(
    :then,
    ->(value) { current.transfer(value, :success) },
    ->(value) { current.transfer(value, :failure) }
  )
  if @loop == current
    raise (
            "JS::Object#await can be called only from RubyVM#evalAsync 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