{"action":"created","issue":{"url":"https://api.github.com/repos/ruby-concurrency/concurrent-ruby/issues/575","repository_url":"https://api.github.com/repos/ruby-concurrency/concurrent-ruby","labels_url":"https://api.github.com/repos/ruby-concurrency/concurrent-ruby/issues/575/labels{/name}","comments_url":"https://api.github.com/repos/ruby-concurrency/concurrent-ruby/issues/575/comments","events_url":"https://api.github.com/repos/ruby-concurrency/concurrent-ruby/issues/575/events","html_url":"https://github.com/ruby-concurrency/concurrent-ruby/issues/575","id":178111377,"number":575,"title":"Wait for all promises to complete?","user":{"login":"schneems","id":59744,"avatar_url":"https://avatars.githubusercontent.com/u/59744?v=3","gravatar_id":"","url":"https://api.github.com/users/schneems","html_url":"https://github.com/schneems","followers_url":"https://api.github.com/users/schneems/followers","following_url":"https://api.github.com/users/schneems/following{/other_user}","gists_url":"https://api.github.com/users/schneems/gists{/gist_id}","starred_url":"https://api.github.com/users/schneems/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/schneems/subscriptions","organizations_url":"https://api.github.com/users/schneems/orgs","repos_url":"https://api.github.com/users/schneems/repos","events_url":"https://api.github.com/users/schneems/events{/privacy}","received_events_url":"https://api.github.com/users/schneems/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-20T16:35:17Z","updated_at":"2016-09-20T18:59:06Z","closed_at":null,"body":"I'm chaining together a bunch of promises in a loop. After the loop is executed I need to wait for them all to finish. I would have thought that calling `wait!` on the parent promise would do this, but it doesn't, instead it only waits for the first promise in the chain to complete.\r\n\r\nCode! I'm building up a bunch of promises in a loop:\r\n\r\n```ruby\r\npromise = nil\r\nexporters.each do |exporter|\r\n  if promise.nil?\r\n    promise = Concurrent::Promise.new(executor: executor) { exporter.call }\r\n  else\r\n    promise.then { exporter.call }\r\n  end\r\nend\r\nif promise\r\n  promise.execute\r\n  concurrent_exporters << promise\r\nend\r\n```\r\n\r\nThen later I want to make sure they're all done:\r\n\r\n```ruby\r\nconcurrent_exporters.each(&:wait!)\r\n```\r\n\r\nBut that doesn't work. The top level promises all have `@state=:fulfilled` however the other promises are stuck at `@state=:pending`. What's the best way to wait for __all__ promises in a chain to be done and raise any errors?"},"comment":{"url":"https://api.github.com/repos/ruby-concurrency/concurrent-ruby/issues/comments/248398811","html_url":"https://github.com/ruby-concurrency/concurrent-ruby/issues/575#issuecomment-248398811","issue_url":"https://api.github.com/repos/ruby-concurrency/concurrent-ruby/issues/575","id":248398811,"user":{"login":"schneems","id":59744,"avatar_url":"https://avatars.githubusercontent.com/u/59744?v=3","gravatar_id":"","url":"https://api.github.com/users/schneems","html_url":"https://github.com/schneems","followers_url":"https://api.github.com/users/schneems/followers","following_url":"https://api.github.com/users/schneems/following{/other_user}","gists_url":"https://api.github.com/users/schneems/gists{/gist_id}","starred_url":"https://api.github.com/users/schneems/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/schneems/subscriptions","organizations_url":"https://api.github.com/users/schneems/orgs","repos_url":"https://api.github.com/users/schneems/repos","events_url":"https://api.github.com/users/schneems/events{/privacy}","received_events_url":"https://api.github.com/users/schneems/received_events","type":"User","site_admin":false},"created_at":"2016-09-20T18:59:06Z","updated_at":"2016-09-20T18:59:06Z","body":"Did figure out you can wait for the last promise, would still be nice to have a top level way of waiting for a promise chain to finish. I don't know when you would ever want to wait on only the top level promise and not the full chain.\r\n\r\nAlso seems silly to add all that extra code just to keep track of the last promise:\r\n\r\n```ruby\r\nexporters.each do |exporter|\r\n  next if exporter.skip?(logger)\r\n\r\n  if !environment.export_concurrent\r\n    exporter.call\r\n    next\r\n  end\r\n\r\n  if promise.nil?\r\n    last_promise = promise = Concurrent::Promise.new(executor: executor) { exporter.call }\r\n  else\r\n    last_promise = promise.then { exporter.call }\r\n  end\r\nend\r\n\r\nif promise\r\n  promise.execute\r\n  concurrent_exporters << last_promise\r\nend\r\n```"}}
{"actor":{"display_login":"schneems"}}