Last active
February 13, 2022 19:24
-
-
Save JadenGeller/bd36ee775e340499a04d7fa06b5f2a59 to your computer and use it in GitHub Desktop.
Create a future from a legacy callback, e.g. `(LPLinkMetadata?, Error?) -> Void`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
extension Future { | |
static func legacy(_ attemptToFulfill: @escaping (@escaping (Output?, Failure?) -> Void) -> Void) -> Future { | |
Future { completion in | |
attemptToFulfill { output, error in | |
switch (output, error) { | |
case (nil, let error?): | |
completion(.failure(error)) | |
case (let output?, nil): | |
completion(.success(output)) | |
case (_?, _?): | |
preconditionFailure("Future.fromCompletionHandler unexpectedly received both non-nil `output` and non-nil `error`") | |
case (nil, nil): | |
preconditionFailure("Future.fromCompletionHandler unexpectedly received both nil `output` and nil `error`") | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's probably better to just resume with https://gist.github.com/JadenGeller/a7f5a96bbca42fdce524d80fe1231c4f