Polling future's result

Is it possible to poll a future for it's result? Looking for something like this:

extension Future {
    // returns `nil` if there's no result yet
    // otherwise returns the readily available result
    var result: Result<Output, Failure>? {
        ... // how?
    }
}

I found the solution:

extension Future {
    var result: Result<Output, Failure>? {
        var result: Result<Output, Failure>?
        _ = sink { error in
            switch error {
                case .finished:
                    break
                case let .failure(error):
                    result = .failure(error)
            }
        } receiveValue: { output in
            result = .success(output)
        }
        return result
    }
}
1 Like