mirror of
https://github.com/easychen/pushdeer.git
synced 2024-09-09 22:48:11 +08:00
40 lines
907 B
Swift
40 lines
907 B
Swift
import Dispatch
|
|
|
|
/**
|
|
Judicious use of `firstly` *may* make chains more readable.
|
|
|
|
Compare:
|
|
|
|
URLSession.shared.dataTask(url: url1).then {
|
|
URLSession.shared.dataTask(url: url2)
|
|
}.then {
|
|
URLSession.shared.dataTask(url: url3)
|
|
}
|
|
|
|
With:
|
|
|
|
firstly {
|
|
URLSession.shared.dataTask(url: url1)
|
|
}.then {
|
|
URLSession.shared.dataTask(url: url2)
|
|
}.then {
|
|
URLSession.shared.dataTask(url: url3)
|
|
}
|
|
|
|
- Note: the block you pass executes immediately on the current thread/queue.
|
|
*/
|
|
public func firstly<U: Thenable>(execute body: () throws -> U) -> Promise<U.T> {
|
|
do {
|
|
let rp = Promise<U.T>(.pending)
|
|
try body().pipe(to: rp.box.seal)
|
|
return rp
|
|
} catch {
|
|
return Promise(error: error)
|
|
}
|
|
}
|
|
|
|
/// - See: firstly()
|
|
public func firstly<T>(execute body: () -> Guarantee<T>) -> Guarantee<T> {
|
|
return body()
|
|
}
|