Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@veganstraightedge
Last active May 20, 2018 23:08
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 veganstraightedge/7873b2dc905e9fe08c115447d6217a70 to your computer and use it in GitHub Desktop.
Save veganstraightedge/7873b2dc905e9fe08c115447d6217a70 to your computer and use it in GitHub Desktop.
promises = []
# Create the collection of promises to do
(1..10).each do |i|
promises << Concurrent::Promise.execute do
thing = get the thing from the API
Thing.create! name: thing.name
end
end
# Do the work in all of the promises: get a thing, save it to the database
promises.map { |p| p.wait }
puts "Things now in the database: #{Thing.count}"
# => 4
# I EXPECT THAT TO BE 10!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment