Swiftpack.co - tiny2n/Await as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by tiny2n.
tiny2n/Await 1.2.0
Asynchronous Programming with async and await base protocol oriented programming
⭐️ 24
🕓 2 years ago
.package(url: "https://github.com/tiny2n/Await.git", from: "1.2.0")

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.

GitHub

link
Stars: 24
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Supported SPM
2 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics