Required Future[Either[Object1, Object2]] found Unit

I have a method in Scala which will return Future[Either[SecurityScanResponse, ScanResult]] However after writing my logic, I am getting required Future[Either[SecurityScanResponse, ScanResult]] found Unit error. Below is my implementation.

def calculateScore(penalties: Int): Future[Either[FootballScore, CricketScore]] = 
calculatePenalties(penalties).onComplete {
 case Success(penalResponse) => 
    if(penalResponse.valid) {
       findNumberOfMatches().onComplete {
                  Left(FootballScore(0, "Valid")))
 case Failure(ex) => 
    log.error("Invalid penalties")
                  Left(FootballScore(0, "Invalid")))

This is just hypothetical code but I am focused upon solution of found Unit required Future[Either[FootballScore, CricketScore]].


  • If you look at the definition of Future.onComplete you would see the following

    abstract def onComplete[U](f: (Try[T]) => U)(implicit executor: ExecutionContext): Unit

    When this future is completed, either through an exception, or a value, apply the provided function.

    If the future has already been completed, this will either be applied immediately or be scheduled asynchronously.

    Note that the returned value of f will be discarded.

    The method onComplete returns a Unit. That's why you are getting that error message.

    You have a Transformation section in the docs that shows different method to apply some operation over a Future. Based on what you showed, I think transform, transformWith, map and flatMap will be the right ones for this case. Not sure how to rewrite your code because it has many functions that are not defined there and also the logic is hard to understand.