A type-safe wrapper for Bolts
github "jinSasaki/SwiftyBolts"
let task: Task<Bool>
task.continueWith { (task) -> Object? in
if let error = task.error {
// Handling error
throw error
}
let result = task.result
// Convert to other object
let object = Object(with: result)
return object
}
let task: Task<Bool>
task.continueOnSuccessWith { (result) -> Bool? in
let success = result ?? false
return success ? "Success!" : "Failure..."
}.continueOnSuccessWith { (result) -> Void in
let message = result ?? ""
print(message)
return
}
func asyncTask() -> Task<String> {
let tcs = TaskCompletionSource<String>()
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) {
tcs.trySet(result: "ended")
}
return tcs.task
}
Task<String>
.forCompletionOfAllTasksWithResults([task1, task2])
.continueWith { (task) -> Void in
let strings: [String] = task.result ?? []
print(strings)
return
}