FuturaAsync

master

Async tools in Swift
kaqu/FuturaAsync

FuturaAsync

Build Status Platforms codebeat badge codecov Swift Package Manager compatible SwiftVersion Contact

Part of Futura tools Project.

Provides promise implementation for iOS, macOS and Linux.

Use via Swift Package Manager

.package(url: "https://github.com/kaqu/FuturaAsync.git", from: "0.9.0"),

Sample usage

let promise = Promise<Int>()
let future = promise.future
future
    .thenSuccess {
        print("Success: \($0)")
    }
    .thenFailure {
        print("Error: \($0)")
    }
    .mapValue {
        return String($0)
    }
    .thenSuccess {
        print("Success(mapped): \($0)")
    }
    .thenFailure {
        print("Error(mapped): \($0)")
    }
    .recoverable { err in
        if (err as? String) == "recoverable" {
            return "Recovery!"
        } else {
            throw err
        }
    }
    .thenSuccess {
        print("Success(mapped, recoverable): \($0)")
    }
    .thenFailure {
        print("Error(mapped, recoverable): \($0)")
    }
    .map {
        switch $0 {
        case let .value(val):
            return val
        case .error:
            return "Errors sometimes happen"
        }
    }
    .then { (val: String) in
        print("Always success(mapped, recoverable, map to Future form FailableFuture): \(val)")
    }

calling

promise.fulfill(with: 9)

prints

Success: 9
Success(mapped): 9
Success(mapped, recoverable): 9
Always success(mapped, recoverable, map to Future form FailableFuture): 9

calling

promise.break() // cancel

prints

Error: cancelled
Error(mapped): cancelled
Error(mapped, recoverable): cancelled
Always success(mapped, recoverable, map to Future form FailableFuture): Errors sometimes happen

calling

promise.break(with: "recoverable" as Error)

prints

Error: recoverable
Error(mapped): recoverable
Success(mapped, recoverable): Recovery!
Always success(mapped, recoverable, map to Future form FailableFuture): Recovery!

Description

  • Swift Tools 4.1.0
View More Packages from this Author

Dependencies

Last updated: Thu Mar 21 2024 05:41:01 GMT-0900 (Hawaii-Aleutian Daylight Time)