fix
杨锴
2025-06-16 3fa53409f5132333ce6d83fff796e108ddd62090
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// FutureExtensions.swift - Copyright 2023 SwifterSwift
 
#if canImport(Combine)
import Combine
 
// MARK: - Methods
 
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Future where Failure == Error {
    /// Creates a `Future` from an `async` throwing function
    /// - Parameter asyncFunc: The asynchronous throwing function to execute.
    convenience init(asyncFunc: @escaping () async throws -> Output) {
        self.init { promise in
            Task {
                do {
                    let result = try await asyncFunc()
                    promise(.success(result))
                } catch {
                    promise(.failure(error))
                }
            }
        }
    }
}
 
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Future where Failure == Never {
    /// Creates a `Future` from an `async` function
    /// - Parameter asyncFunc: The asynchronous function to execute.
    convenience init(asyncFunc: @escaping () async -> Output) {
        self.init { promise in
            Task {
                let result = await asyncFunc()
                promise(.success(result))
            }
        }
    }
}
 
#endif