Sha256: 4cee01f213f7325e980ce2baa95d15989a093028c0736b08cf6f3e06a437af26

Contents?: true

Size: 627 Bytes

Versions: 3

Compression:

Stored size: 627 Bytes

Contents

# Referenced from {Qiita}[http://qiita.com/south37/items/99a60345b22ef395d424]
class Promise
  def initialize(callback)
    @callback = callback
  end

  def then(resolve = ->() {}, reject = ->() {})
    @callback.call(resolve, reject)
  end
end

def async_internal(fiber)
  chain = ->(result) {
    return if result.class != Promise
    result.then(->(val) {
        chain.call(fiber.resume(val))
    })
  }
  chain.call(fiber.resume)
end

def async(method_name, &block)
  define_singleton_method method_name, ->(*args) {
    async_internal(Fiber.new {block.call(*args)})
  }
end

def await(promise)
  Fiber.yield promise
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
em-midori-0.0.9.1 lib/em-midori/promise.rb
em-midori-0.0.9 lib/em-midori/promise.rb
em-midori-0.0.8 lib/em-midori/promise.rb