title: Combining Futures (Twitter) and Either in Scala
body: <p>We use Twitter futures (as part of the Finagle stack) and I don't like the concept of using (business) exceptions to control the flow of our application, because exceptions don't show up in method signatures.</p>
  
  <p>So I had the idea to use Future[Either[A,B]] as a replacement.</p>
  
  <p>But I have some problems in using for comprehensions over futures with this concept:</p>
  
  <p>E.g. we have a repository method:</p>
  
  <pre><code>def getUserCredentialsByNickname(nickname: String): Future[Either[EntityNotFound, UserCredentials]]
  </code></pre>
  
  <p>and a handler method which uses this repo and does some other checks and also creates a token</p>
  
  <pre><code>def process(request: LoginRequest): Future[Either[Failure, Login]] = {
        for {
          credentialsEither &lt;- userRepository.getUserCredentialsByNickname(request.username)
          ...several other calls/checks which should 'interrupt' this for comprehension
          token &lt;- determineToken(credentials)
  } yield token
  </code></pre>
  
  <p>The calls in the for comprehension after the getUserCredentialsByNickname(..) should only be executed if this call returns a Right[UserCredentials], but also the detailed error information from each returned Either should be returned from the handler.</p>