8000 Missing combinator: whenAllResult · Issue #24 · tomlokhorst/Promissum · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Missing combinator: whenAllResult #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tomlokhorst opened this issue Aug 30, 2016 · 0 comments
Open

Missing combinator: whenAllResult #24

tomlokhorst opened this issue Aug 30, 2016 · 0 comments

Comments

@tomlokhorst
Copy link
Owner
tomlokhorst commented Aug 30, 2016
public func whenAllResult<Value, Error>(promises: [Promise<Value, Error>]) -> Promise<[Result<Value, Error>], NoError> {
  let source = PromiseSource<[Result<Value, Error>], NoError>()
  var results = promises.map { $0.result }
  var remaining = promises.count

  if remaining == 0 {
    source.resolve([])
  }

  for (ix, promise) in promises.enumerate() {

    promise
      .finallyResult { result in
        results[ix] = result
        remaining = remaining - 1

        if remaining == 0 {
          source.resolve(results.map { $0! })
        }
    }
  }

  return source.promise
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0