Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created October 1, 2020 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/f267ab57b01f009c356980c688dc499d to your computer and use it in GitHub Desktop.
Save havenwood/f267ab57b01f009c356980c688dc499d to your computer and use it in GitHub Desktop.
require 'async'
require 'async/barrier'
require 'open-uri'
def async_fetch(*urls)
barrier = Async::Barrier.new
Async do
urls.map do |url|
barrier.async { URI.open url, &:read }
end.tap { barrier.wait }
end.result.map(&:result)
end
url = 'https://api.kanye.rest?format=text'
puts async_fetch url, url, url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment