Sha256: 9f8897bcea38a44a377a4bee93d0007f4dcfc3d61a8e5fc3088a03ef29ee945b

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

# helpers: coerce_to
# await: true

if `Opal.config.experimental_features_severity == 'warning'`
  warn 'Await functionality is a technology preview, which means it may change its behavior ' \
      'in the future unless this warning is removed. If you are interested in this part, ' \
      'please make sure you track the async/await/promises tag on Opal issues: ' \
      'https://github.com/opal/opal/issues?q=label%3Aasync%2Fawait%2Fpromises'
end

%x{
  var AsyncFunction = Object.getPrototypeOf(async function() {}).constructor;
}

require 'promise/v2'

class Array
  def map_await(&block)
    i = 0
    results = []
    while i < `self.length`
      results << yield(self[i]).__await__
      i += 1
    end
    results
  end

  def each_await(&block)
    map_await(&block).__await__
    self
  end
end

module Enumerable
  def each_async(&block)
    PromiseV2.when(*map(&block)).__await__
  end
end

module Kernel
  # Overwrite Kernel.exit to be async-capable.
  def exit(status = true)
    $__at_exit__ ||= []

    until $__at_exit__.empty?
      block = $__at_exit__.pop
      block.call.__await__
    end

    %x{
      if (status.$$is_boolean) {
        status = status ? 0 : 1;
      } else {
        status = $coerce_to(status, #{Integer}, 'to_int')
      }

      Opal.exit(status);
    }
    nil
  end

  def sleep(seconds)
    prom = PromiseV2.new
    `setTimeout(#{proc { prom.resolve }}, #{seconds * 1000})`
    prom
  end
end

module Kernel
  alias await itself
end

class Proc
  def async?
    `self instanceof AsyncFunction`
  end
end

class Method
  def async?
    @method.async?
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
opal-1.5.1 stdlib/await.rb
opal-1.5.0 stdlib/await.rb
opal-1.5.0.rc1 stdlib/await.rb
opal-1.4.1 stdlib/await.rb
opal-1.4.0 stdlib/await.rb
opal-1.4.0.alpha1 stdlib/await.rb
opal-1.3.2 stdlib/await.rb
opal-1.3.1 stdlib/await.rb
opal-1.3.0 stdlib/await.rb
opal-1.3.0.rc1 stdlib/await.rb
opal-1.3.0.alpha1 stdlib/await.rb