Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created October 30, 2012 14:01
Show Gist options
  • Save arturaz/3980328 to your computer and use it in GitHub Desktop.
Save arturaz/3980328 to your computer and use it in GitHub Desktop.
private[this] def picker[Question, Yes, No, Value](
question: Question, onSuccess: Yes => Left[Value, Unit]
)(
implicit mYes: Manifest[Yes], mNo: Manifest[No]
): Future[Either[Value, Unit]] = {
// Promise for fallbacking that we're never going to fulfill.
val unfulfilledPromise = Promise()
val questions = actors.map { actor => actor ? question }
val successQuestions = questions.map {
_ mapTo mYes map onSuccess fallbackTo(unfulfilledPromise)
}
val success= Future.firstCompletedOf(successQuestions)
val failureQuestions = questions.map { _ mapTo mNo }
val failure = Future.sequence(failureQuestions) map { _ => Right() }
Future.firstCompletedOf(Seq(success, failure))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment