Last active
May 10, 2018 01:07
-
-
Save enomoto/9fb835e619a23c27b2bbf2b74fb89338 to your computer and use it in GitHub Desktop.
PromiseKit 6 example with Xcode 9.2 and Swift 4.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PromiseKit | |
struct YoRepository { | |
func fetchDataWithPromise() -> Promise<String> { | |
return Promise { seal in | |
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .milliseconds(500)) { | |
guard true else { | |
// failure | |
seal.reject(MyError.unknown) | |
return | |
} | |
// success | |
seal.fulfill("yo! fulfill called") | |
} | |
} | |
} | |
} | |
struct YoUseCase { | |
func fetchYo() { | |
firstly { | |
YoRepository().fetchDataWithPromise() | |
}.done { result in | |
print(result) | |
}.catch { error in | |
print("some kind of error getting yo! -> \(error)") | |
} | |
} | |
} | |
enum MyError: Error { | |
case unknown | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment