等待对 Foundation 的 Operation
的支持。
struct OperationNotStarted : Error {}
final class MyOperation : Operation, HasResult {
var result: Result<Int, Error> = .failure(OperationNotStarted())
override func main() {
result = .success(42)
}
}
let q = OperationQueue()
let op = MyOperation()
/* op.result is a failure, set to OperationNotStarted() */
let r = try await q.addOperationAndGetResult(op)
/* op.result is now a success, equal to r (equal to 42) */