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
577 B

  1. //
  2. // Async.swift
  3. // then
  4. //
  5. // Created by Sacha Durand Saint Omer on 13/03/2017.
  6. // Copyright © 2017 s4cha. All rights reserved.
  7. //
  8. import Foundation
  9. import Dispatch
  10. @discardableResult
  11. public func async<T>(block:@escaping () throws -> T) -> Async<T> {
  12. let p = Promise<T> { resolve, reject in
  13. DispatchQueue(label: "then.async.queue", attributes: .concurrent).async {
  14. do {
  15. let t = try block()
  16. resolve(t)
  17. } catch {
  18. reject(error)
  19. }
  20. }
  21. }
  22. p.start()
  23. return p
  24. }