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.

28 lines
657 B

  1. //
  2. // Promise+Finally.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. public func finally(_ block: @escaping () -> Void) {
  11. tryStartInitialPromiseAndStartIfneeded()
  12. registerFinally(block)
  13. }
  14. public func registerFinally(_ block: @escaping () -> Void) {
  15. synchronize { state, blocks in
  16. switch state {
  17. case .rejected, .fulfilled:
  18. block()
  19. case .dormant, .pending:
  20. blocks.finally.append(block)
  21. }
  22. }
  23. }
  24. }