简体   繁体   中英

scala-cats EitherT: chaining futures

I am following this . Structure of my program is:

(for {
        data <- myService.fetch(id).eitherT
        values <- anotherService.fetch(data.id).eitherT
        // todo: process values as part of forcomprehension
      } yield data.id).value

myService.fetch returns Future[...].

anotherService.fetch also returns Future[...], and values is a Seq. I want to process entries in values using foreach

values.foreach(value => service.moreAction(value))

moreAction also returns Future[...]

Hence my aim is to chain it with previous Futures in for-comprehension itself.

What is idiomatic way of doing it?

In Cats if you want to go through all values of some data, apply some effect to them, and then get a data within this effect:

DataOf[A] => (A => Effect[B]) => Effect[DataOf[B]]

you use Traverse type class with its extension methods.

(Think of it like of generalization of Future.sequence which takes Seq[Future[A]] and returns Future[Seq[A]] , but one which performs seq.map(toFuture) and Future.sequence in one step).

import cats.syntax.traverse._

for {
  data <- myService.fetch(id).eitherT
  values <- anotherService.fetch(data.id).eitherT
  // Traverse is not available for Seq, it requires some specific data e.g. List
  result <- values.toList.traverse(value => service.moreAction(value).eitherT)
} yield result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM