Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Last active February 13, 2022 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/bd36ee775e340499a04d7fa06b5f2a59 to your computer and use it in GitHub Desktop.
Save JadenGeller/bd36ee775e340499a04d7fa06b5f2a59 to your computer and use it in GitHub Desktop.
Create a future from a legacy callback, e.g. `(LPLinkMetadata?, Error?) -> Void`
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`")
}
}
}
}
}
@JadenGeller
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment