Search code examples
scalafunctional-programmingscala-catscats-effect

Timeout for Type class Async


I am relatively new to cats effect. We are currently using Cats effect 2.x.

I would like to understand how can we apply timeout for Async Type class.

def myMethod[F[_]: Async, A]: EitherT[F, Error, A] = {
  val computation: EitherT[F, Error, A] = ???  
  val fallback: EitherT[F, Error, A] = ???

  //How to Execute computation with a timeout of 1 second and recover with fallback ??

}

I could do this for the IO with timeoutTo but not sure how can we do this for Async


Solution

  • For both CE2 and CE3 the answer is the same (though the import may differ slightly);

    import cats.effect.syntax.all._
    
    computation.timeoutTo(duration, fallback)