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

Version Path
async-await-0.5.0 examples/chickens.rb
async-await-0.4.0 examples/chickens.rb
async-await-0.3.0 examples/chickens.rb
async-await-0.2.0 examples/chickens.rb
async-await-0.1.0 examples/chickens.rb