PromiseKit
主要用于解决回调地狱, 类似于RxSwift的子集, 比RxSwift更加简单.
PromiseKit的核心思想就是Promise. Promise是一个承诺, 对应的是一个未来(Future), 在未来这个承诺可能兑现(fulfill)或者被拒绝(rejectd).
通过构建Promise, 然后使用<code>firstly</code>, <code>then</code>, <code>done</code>, <code>catch</code>等关键字控制Promise的兑现.
构建Promise
func myPromise() -> Promise<Int> {
return Promise { seal in
seal.fulfill(3)
}
}
构建Guarantee
func myGuarantee() -> Guarantee<Int> {
return Guarantee.value(4)
}
兑现
firstly {
myPromise1()
}.then { result in
myPromise2()
}.done { result in
print("🍉3 \(result)")
}.catch { error in
print("error")
}
AwaitKit
基于PromiseKit, 用起来更加清爽.
func test() -> Promise<Int> {
return async {222}
}
_ = try? await(test1())
_ = try? await(test2())
结束语
虽然AwaitKit避免了回调地狱, 但是破坏了代码结构, 有点得不偿失吧.
网友评论