This file contains hidden or 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
fetchJSON(fromURL: "http://api.json") { (json, error) in | |
} |
This file contains hidden or 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
fetchJSON(fromURL: "someURL") { (response, results, error) in | |
switch response { | |
case .success: | |
fetchUserData(for: results) { (response, results, error) in | |
switch response { | |
case .success: | |
loginUser(user: results) { (response, results, error) in | |
switch response { | |
case .success: | |
// finialy all of the callbacks are finished |
This file contains hidden or 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
fetchJSON(fromURL: "someURL") { (response, results, error) in | |
} |
This file contains hidden or 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
func promiseFetchJSON() -> Promise<[String: Any]> { | |
return Promise<[String: Any]> { seal in | |
fetchJSON(fromURL: "someURL", completion: { (response, results, error) in | |
switch response { | |
case .success: | |
seal.fulfill(results) | |
case .fail: | |
seal.reject(error) | |
} | |
}) |
This file contains hidden or 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
func promiseFetchJSON() -> Promise<[String: Any]> { | |
return Promise<[String: Any]> { seal in | |
fetchJSON(fromURL: "someURL", completion: { (response, results, error) in | |
switch response { | |
case .success: | |
seal.fulfill(results) | |
case .fail: | |
seal.reject(error) | |
} | |
}) |
This file contains hidden or 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
func promiseFetchUserData(user: [String: Any]) -> Promise<User> { | |
return Promise<User> { seal in | |
fetchUserData(for: user, completion: { (response, results, error) in | |
switch response { | |
case .success: | |
seal.fulfill(results) | |
case .fail: | |
seal.reject(error) | |
} | |
}) |
This file contains hidden or 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
firstly { | |
promise1() | |
}.then { | |
promise2() | |
}.then { | |
promise3() | |
}.ensure { | |
// something that should happen whatever the outcome | |
}.done { | |
// all promises are resolved |
This file contains hidden or 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
firstly { | |
promiseFetchJSON() | |
}.then { (json) in | |
promiseFetchUserData(user: json) | |
}.then { (user) in | |
promiseLoginUser(user: user) | |
}.ensure { | |
// something that should happen whatever the outcome | |
}.done { (success) in | |
// all promises are resolved |
This file contains hidden or 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
private func downloadPhotos() { | |
// 1. Show preloading animation | |
self.activityIndicator.startAnimating() | |
// 2. Fetch and serialize json response | |
Alamofire.request(photosURL, method: .get).validate().responseData { (data) in | |
guard let data = data.result.value else { return } | |
guard let photos = try? JSONDecoder().decode([Photo].self, from: data) else { return } | |
// 3. Download photos | |
for photo in photos { | |
guard let photoUrl = URL(string: photo.url) else { return } |
This file contains hidden or 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
private func function() -> Promise<T> { | |
return Promise<T> { seal in | |
} | |
} |
OlderNewer