Skip to content

Instantly share code, notes, and snippets.

@cloud8421
Last active August 29, 2015 14:02
Show Gist options
  • Save cloud8421/ee8f36eb74224ded9aa5 to your computer and use it in GitHub Desktop.
Save cloud8421/ee8f36eb74224ded9aa5 to your computer and use it in GitHub Desktop.
Lightweight futures in Ruby (with Rails example)
class Future < Thread
def result
join
value
end
end
class HomeController < ApplicationController
def index
@page = HomePage.new
end
end
class HomePage
attr_reader :data
def initialize
@data = {
featured_items: Future.new { http_fetch(:featured_items) },
grid_items: Future.new { http_fetch(:grid_items) }
}
end
def featured_items
@featured_items ||= data[:featured_items].result
end
def grid_items
@grid_items ||= data[:grid_items].result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment