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.

34 lines
880 B

  1. //
  2. // Promise+Error.swift
  3. // then
  4. //
  5. // Created by Sacha Durand Saint Omer on 20/02/2017.
  6. // Copyright © 2017 s4cha. All rights reserved.
  7. //
  8. import Foundation
  9. public extension Promise {
  10. @discardableResult public func onError(_ block: @escaping (Error) -> Void) -> Promise<Void> {
  11. tryStartInitialPromiseAndStartIfneeded()
  12. return registerOnError(block)
  13. }
  14. @discardableResult public func registerOnError(_ block: @escaping (Error) -> Void) -> Promise<Void> {
  15. let p = Promise<Void>()
  16. passAlongFirstPromiseStartFunctionAndStateTo(p)
  17. syncStateWithCallBacks(
  18. success: { _ in
  19. p.fulfill(())
  20. },
  21. failure: { e in
  22. block(e)
  23. p.fulfill(())
  24. },
  25. progress: p.setProgress
  26. )
  27. p.start()
  28. return p
  29. }
  30. }