Skip to content
This repository was archived by the owner on Jan 15, 2019. It is now read-only.

kaqu/FuturaAsync

Repository files navigation

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!

About

Async tools in Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages