Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created February 4, 2017 11:17
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 malcommac/fad095cf4d09f9b3adef36fe4e2a1793 to your computer and use it in GitHub Desktop.
Save malcommac/fad095cf4d09f9b3adef36fe4e2a1793 to your computer and use it in GitHub Desktop.
Hydra_Internals_12.swift
public func map_series<A, B, S: Sequence>(context: Context, items: S, transform: @escaping (A) throws -> Promise<B>) -> Promise<[B]> where S.Iterator.Element == A {
let initial = Promise<[B]>(resolved: [])
return items.reduce(initial) { chain, item in
return chain.then(in: context) { results in
try transform(item).then(in: context) { results + [$0] }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment