Skip to content
/ Await Public

Asynchronous Programming with async and await base protocol oriented programming

License

Notifications You must be signed in to change notification settings

tiny2n/Await

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Language Platform

Await

Overview

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

Introducation

__Requires Swift 3 or later

Installation

Add the files to your project manually by dragging the Await directory into your Xcode project.

or

CocoaPods:

pod 'Await', :git => 'https://github.com/tiny2n/Await.git'

Usage

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)")
  }
}
        



License

Await is available under the MIT license. See the LICENSE file for more info.

About

Asynchronous Programming with async and await base protocol oriented programming

Resources

License

Stars

Watchers

Forks

Packages

No packages published