目录
目录README.md

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!
关于
136.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号