ruby 中是否提供 promise 概念?
Is promise concept available in ruby?
我只是想知道,ruby 中是否有链接概念。
我想一个接一个地执行一系列异步任务或方法。可能吗?
谢谢,
拉维
您可能想要创建一个进程 class,例如:
class MyProcess
PROCESS_STEPS = %w(
step_one
step_two
step_three
)
class << self
def next_step
new.next_step
end
end # Class Methods
#======================================================================
# Instance Methods
#======================================================================
def next_step
PROCESS_STEPS.each do |process_step|
send(process_step) if send("do_#{process_step}?")
end
end
def step_one
# execute step one task
end
def do_step_one?
# some logic
end
def step_two
# execute step two task
end
def do_step_two?
# some logic
end
def step_three
# execute step three task
end
def do_step_three?
# some logic
end
end
你可能会把它放在:
app
|- processes
| |- my_process.rb
然后,在每个任务结束时,执行如下操作:
MyProcess.next_step
Javascript,首次引入 Promises 的地方也是同步的,promises 是严格意义上对回调的抽象
有 Ruby 的并发库,其中一些在一定程度上体现了 Promises 的精神,google 搜索 promise.rb
会产生一些有希望的结果:
https://github.com/lgierth/promise.rb
https://github.com/ruby-concurrency/concurrent-ruby
也许这些不是惯用的ruby,但它们确实提供了一些有用的范例
据我所知,promise.rb
是最常用的 gem 异步机制,遵循 js Promise/A+ 标准。
这篇文章介绍得不错:https://medium.com/@gauravbasti2006/lets-keep-our-promise-in-ruby-e45925182fdc
concurrent-ruby 与其他广泛使用的语言类似,最广泛用于实现与并发相关的功能,如 promises。文档也非常简单:
https://github.com/ruby-concurrency/concurrent-ruby/blob/master/docs-source/promises.in.md
要链接异步任务,您可以使用以下方法:
https://github.com/ruby-concurrency/concurrent-ruby/blob/master/docs-source/promises.in.md#chaining
我只是想知道,ruby 中是否有链接概念。 我想一个接一个地执行一系列异步任务或方法。可能吗?
谢谢, 拉维
您可能想要创建一个进程 class,例如:
class MyProcess
PROCESS_STEPS = %w(
step_one
step_two
step_three
)
class << self
def next_step
new.next_step
end
end # Class Methods
#======================================================================
# Instance Methods
#======================================================================
def next_step
PROCESS_STEPS.each do |process_step|
send(process_step) if send("do_#{process_step}?")
end
end
def step_one
# execute step one task
end
def do_step_one?
# some logic
end
def step_two
# execute step two task
end
def do_step_two?
# some logic
end
def step_three
# execute step three task
end
def do_step_three?
# some logic
end
end
你可能会把它放在:
app
|- processes
| |- my_process.rb
然后,在每个任务结束时,执行如下操作:
MyProcess.next_step
Javascript,首次引入 Promises 的地方也是同步的,promises 是严格意义上对回调的抽象
有 Ruby 的并发库,其中一些在一定程度上体现了 Promises 的精神,google 搜索 promise.rb
会产生一些有希望的结果:
https://github.com/lgierth/promise.rb https://github.com/ruby-concurrency/concurrent-ruby
也许这些不是惯用的ruby,但它们确实提供了一些有用的范例
据我所知,promise.rb
是最常用的 gem 异步机制,遵循 js Promise/A+ 标准。
这篇文章介绍得不错:https://medium.com/@gauravbasti2006/lets-keep-our-promise-in-ruby-e45925182fdc
concurrent-ruby 与其他广泛使用的语言类似,最广泛用于实现与并发相关的功能,如 promises。文档也非常简单: https://github.com/ruby-concurrency/concurrent-ruby/blob/master/docs-source/promises.in.md
要链接异步任务,您可以使用以下方法: https://github.com/ruby-concurrency/concurrent-ruby/blob/master/docs-source/promises.in.md#chaining