title: Play Framework Controller Handling Multiple Futures
body: <p>I have a Play controller that takes in a Model object that comes in from the user interface. This model object is a User that I'm trying to insert into the database. But before I insert this User in the table, I check if there is a duplicate user that already exist for the given EMail address. If yes, I reject the request and if not I insert. I'm using Slick and Play framework and here is my controller code:</p>
  
  <pre><code>  def registerNewUser(user: User) = {
      dbService.registerNewUser(User.toUserRow(user))
        .map(userID =&gt; Ok(Json.obj("status" -&gt; "ok", "userId" -&gt; userID)))
        .recover { case ex =&gt; InternalServerError(Json.obj("error" -&gt; s"${ex.getMessage}")) }
    }
  
    def createUser() = Action.async(parse.tolerantJson) { request =&gt;
      request.body.validate[User].fold(
        errors =&gt; Future.successful {
          BadRequest(Json.obj("status" -&gt; "error", "message" -&gt; JsError.toJson(errors)))
        },
        user =&gt; {
          val userExists: Future[Boolean] = dbService.userExists(user.email)
          userExists.map(value =&gt; {
            if (value) UnprocessableEntity(Json.obj("status" -&gt; "error", "message" -&gt; s"user with email ${user.email.toString} already exists"))
            else registerNewUser(user)
          }).recover {
            case ex =&gt; InternalServerError(Json.obj("error" -&gt; s"${ex.getMessage}"))
          }
        } // compilation failure on this line [[ Line X ]]
      )
    }
  </code></pre>
  
  <p>I do not see anything wrong with this approach, but my compiler is not happy. It complaints at Line X as:</p>
  
  <pre><code>Expression of type Future[Object] does not confirm to the expected type _X
  </code></pre>
  
  <p>What is actually the problem here?</p>