You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
680 B

  1. //
  2. // Promise+Race.swift
  3. // then
  4. //
  5. // Created by Sacha Durand Saint Omer on 22/02/2017.
  6. // Copyright © 2017 s4cha. All rights reserved.
  7. //
  8. import Foundation
  9. extension Promises {
  10. /// `Promise.race(p1, p2, p3, p4...)`Takes the state of the fastest returning promise.
  11. /// If the first fails, it fails. If the first resolves, it resolves.
  12. public static func race<T>(_ promises: Promise<T>...) -> Promise<T> {
  13. return Promise { resolve, reject in
  14. for p in promises {
  15. p.then { t in
  16. resolve(t)
  17. }.onError { e in
  18. reject(e)
  19. }
  20. }
  21. }
  22. }
  23. }