Skip to content

Instantly share code, notes, and snippets.

View s4cha's full-sized avatar
🎯
Focusing

Sacha DSO s4cha

🎯
Focusing
View GitHub Profile
struct Profile {
var identifier = 0
var name = ""
var stats = Stats()
}
fetchUserId({ id in
fetchUserNameFromId(id, success: { name in
fetchUserFollowStatusFromName(name, success: { isFollowed in
// The three calls in a row succeeded YAY!
reloadList()
}, failure: { error in
// Fetching user ID failed
reloadList()
})
}, failure: { error in
fetchUserId()
.then(fetchUserNameFromId)
.then(fetchUserFollowStatusFromName)
.then(updateFollowStatus)
.onError(showErrorPopup)
.finally(reloadList)
func fetchUserId() -> Promise<Int> {
return Promise { resolve, reject in
doSomethingAsync { resolve(object: userId) }
}
}
fetchUserId().then { id in
print("UserID : \(id)")
}.onError { e in
print("An error occured : \(e)")
}.finally {
print("Everything is Done :)")
}
fetchUserId()
.then(printUserID)
.onError(showErrorPopup)
.finally(reloadList)
diff --git a/Source/Promise.swift b/Source/Promise.swift
index f18c6ca..3186f03 100644
--- a/Source/Promise.swift
+++ b/Source/Promise.swift
@@ -39,11 +39,13 @@ public class Promise<T> {
public init(callback: @escaping (_ resolve: @escaping ResolveCallBack,
_ reject: @escaping RejectCallBack) -> Void) {
+ print("+")
promiseCallBack = callback
@s4cha
s4cha / Api.swift
Last active October 17, 2016 16:37
import ws
import then // Needed to import Promise Type
import Arrow // Needed to import JSON Type
let api = Api() // Define global api
struct Api {
// Connect to the base URL
private let ws = WS("http://jsonplaceholder.typicode.com")
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
api.getUsers().then { json in
print(json)
}
import Arrow
extension User: ArrowParsable {
mutating func deserialize(_ json: JSON) {
username <-- json["username"]
}
}