Вот мой код:
import Foundation
func test() async throws {
// ---> Error: Generic parameter 'Failure' could not be inferred
let result = try await Task.withTimeout(after: .seconds(0.1)) {
try await test2()
}
}
func test2() async throws -> Int {
}
extension Task {
static func withTimeout(
after duration: Duration,
operation: @escaping @Sendable () async throws -> T
) async throws -> T {
try await withThrowingTaskGroup(of: T.self) { group in
group.addTask {
try await operation()
}
group.addTask {
try await Task.sleep(for: duration) // Error ----> Referencing static method 'sleep(for:tolerance
throw CancellationError()
}
guard let result = try await group.next() else {
throw CancellationError()
}
group.cancelAll()
return result
}
}
}
< /code>
Это две ошибки компиляции, которые я получаю: < /p>
Generic parameter 'Failure' could not be inferred
Referencing static method 'sleep(for:tolerance
< /code>
Но я не понимаю этих ошибок. Если у кого -то есть идея ..
Редактировать: Нашел решение здесь: https://gist.github.com/swhitty/9be89df ... 916273bbb9, может кто -то не удалять это, потому что это поможет другим.>
Подробнее здесь: https://stackoverflow.com/questions/796 ... -extension