Result -> Either change
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
promise.break() // cancel
Error: cancelled Error(mapped): cancelled Error(mapped, recoverable): cancelled Always success(mapped, recoverable, map to Future form FailableFuture): Errors sometimes happen
promise.break(with: "recoverable" as Error)
Error: recoverable Error(mapped): recoverable Success(mapped, recoverable): Recovery! Always success(mapped, recoverable, map to Future form FailableFuture): Recovery!
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
FuturaAsync
Part of Futura tools Project.
Provides promise implementation for iOS, macOS and Linux.
Use via Swift Package Manager
Sample usage
calling
prints
calling
prints
calling
prints