Await and async in Crystal

https://github.com/anykeyh/await_async

Usage

  • Call async on any method or block to create a MiniFuture
  • Call await on any MiniFuture to wait for/get the result
  • Conveniently, you can call await on iterators (eg. Array) .
  • Can improve drastically application which relay on blocking IO like web API or file writing.
def fetch_websites_async
  %w(www.github.com
  www.yahoo.com
  www.facebook.com
  www.twitter.com
  crystal-lang.org).map do |ws|
    async do
      HTTP::Client.get "https://#{ws}"
    end
  end
end

# Process the websites concurrently. Start querying another website when the
# first one is waiting for response
await(fetch_websites_async).each do |response|
 #...
end