Last active
August 29, 2015 14:14
-
-
Save jamesshipton/abe8015394754f9ed9ab to your computer and use it in GitHub Desktop.
synchronously.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Synchronously | |
class Fetcher | |
def self.fetch_api_data(uuids) | |
uuids.map { |uuid| new(uuid).fetch } | |
end | |
def fetch | |
"api response for #{uuid}" | |
end | |
private | |
attr_accessor :uuid | |
def initialize(uuid) | |
self.uuid = uuid | |
end | |
end | |
# Asynchronously | |
class Fetcher | |
include Celluloid | |
def self.fetch_api_data(uuids) | |
actors = uuids.map { |uuid| new(uuid) } | |
responses = actors.map(&:future).map(&:fetch).map(&:value) | |
actors.map(&:terminate) | |
responses | |
end | |
def fetch | |
"api response for #{uuid}" | |
end | |
private | |
attr_accessor :uuid | |
def initialize(uuid) | |
self.uuid = uuid | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment