Sha256: 3c3a1e113a5b901fe758561a3c3fafa7b24830b6b1cef53d72327cd4df671ee1
Contents?: true
Size: 679 Bytes
Versions: 5
Compression:
Stored size: 679 Bytes
Contents
require_relative '../lib/async/await' class Coop include Async::Await async def count_chickens(area_name) 3.times do |i| sleep rand puts "Found a chicken in the #{area_name}!" end end async def find_chicken(areas) puts "Searching for chicken..." sleep rand * 5 return areas.sample end async def count_all_chickens # These methods all run at the same time. count_chickens("garden") count_chickens("house") count_chickens("tree") # Wait for all previous async work to complete... barrier! puts "There was a chicken in the #{find_chicken(["garden", "house", "tree"]).wait}" end end coop = Coop.new coop.count_all_chickens
Version data entries
5 entries across 5 versions & 1 rubygems