Asynchronous Programming with async and await base protocol oriented programming
Await is a powerful Swift library
C# async and await https://msdn.microsoft.com/en-us//library/hh191443(v=vs.110).aspx
__Requires Swift 3 or later
Add the files to your project manually by dragging the Await directory into your Xcode project.
or
pod 'Await', :git => 'https://github.com/tiny2n/Await.git'
write this:
// async and await
async {
let result = try await(AwaitExecute()) // AwaitCompletable Type
...
}
AwaitCompletable protocol this:
// AwaitCompletable
public protocol AwaitCompletable {
associatedtype AwaitCompletableType
var queue: DispatchQueue { get }
var timeout: DispatchTimeInterval? { get }
func execute(_ completion: @escaping (AwaitCompletableResult<AwaitCompletableType>) -> Void)
}
Custom this:
// Custom Await Completable
public struct AsyncObjectTask: AwaitCompletable {
func execute(_ completion: @escaping (AwaitCompletableResult<AwaitCompletableType>) -> Void) {
// execute task
<#async code#>
completion(.success(<#await completable type#>))
// or completion(.failure(<#error#>))
}
}
example this:
// async block is Asynchronous
// await block is Synchronous
// serial call
async {
do {
let odd = try await(ACSum0To1000000000Odd())
let even = try await(ACSum0To1000000000Even())
let sum = try await(ACResultSum(odd, even))
print(">>> odd: \(odd)")
print(">>> even: \(even)")
print(">>> sum: \(sum)")
}
catch AwaitError.nil {
print("throw nil")
}
catch AwaitError.timeout {
print("throw timeout")
}
catch {
print("thorw unknown")
}
}
// using subclassing by class AwaitConcurrentSupport<T>
// concurrent call
async {
do {
let result = try await(ACSum0To1000000000Even(), ACSum0To1000000000Odd())
print(">>> count: \(result.count)")
print(">>> even: \(result[1])")
print(">>> odd: \(result[0])")
}
catch {
// Type: AwaitConcurrentError.concurrent<T>
print("[Error] \(error)")
}
}
Await is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 24 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics