Just tried it out. Async blocks evaluate to Task objects which have a wait method and a result attribute which evaluates to the value of the block.
require 'async'
res = Async do |task|
name_task = task.async do
sleep 2
"Jenny"
end
task.async do
sleep 5
9
end
"Hello #{name_task.wait}"
end
puts res # => "Hello Jenny" after 5 seconds.
After using Python's and JS's async implementations this seems beautiful by comparison. Here's the a rough Python equivalent.
import asyncio
async def get_name():
await asyncio.sleep(2)
return "Jenny"
async def get_number():
await asyncio.sleep(5)
return 7
async def main():
number_task = get_number()
name_task = get_name()
name, _ = await asyncio.gather(name_task, number_task)
return f"Hello {name}"
print(asyncio.run(main()))