Search code examples
scalascalazscala-cats

How to call Await.ready for function returning EitherT[Future,Failure,Result]


(1) I have a function returns EitherT as

createData(m):EitherT[Future, Failure, Result]

(2) I want to do the following transformation sequentially so that wait for each call to createData complete one by one.

val futures = group.map { m => Future {createData(m)}}
Await.ready(Future.sequence(futures),10)

(3) However, Await.ready requires Future to pass in, so in the above code, I wrapped EitherT with Future. But I think the above code (2) does not do what I want do. Since Await just for Outer Wrapper Future and it does not care if the inner true Future for createData is completed or not.

My Question:

How should I invoke Await in this case (EitherT Future) to wait for each inner sequential call to createData completed one by one ?


Solution

  • .value returns your untransformed type. F is a Future in your case:

    val value: F[Either[A, B]]
    

    This means your issue should boil down to (types just for illustration):

    val futures: List[Future[Either[Failure, Result]]] = group.map {m => createData(m).value}
    val results: List[Either[Failure, Result]] = Await.ready(Future.sequence(futures), 10)