Future[Option] in Scala for-comprehensions

前端 未结 5 1670
闹比i
闹比i 2020-12-07 19:16

I have two functions which return Futures. I\'m trying to feed a modified result from first function into the other using a for-yield comprehension.

This approach w

5条回答
  •  旧时难觅i
    2020-12-07 19:22

    This answer to a similar question about Promise[Option[A]] might help. Just substitute Future for Promise.

    I'm inferring the following types for getUserDetails and getSchool from your question:

    getUserDetails: UserID => Future[Either[??, UserDetails]]
    getSchool: SchoolID => Future[Option[School]]
    

    Since you ignore the failure value from the Either, transforming it to an Option instead, you effectively have two values of type A => Future[Option[B]].

    Once you've got a Monad instance for Future (there may be one in scalaz, or you could write your own as in the answer I linked), applying the OptionT transformer to your problem would look something like this:

    for {
      ud  <- optionT(getUserDetails(user.userID) map (_.right.toOption))
      sid <- optionT(Future.successful(ud.schoolID))
      s   <- optionT(getSchool(sid))
    } yield s
    

    Note that, to keep the types compatible, ud.schoolID is wrapped in an (already completed) Future.

    The result of this for-comprehension would have type OptionT[Future, SchoolID]. You can extract a value of type Future[Option[SchoolID]] with the transformer's run method.

提交回复
热议问题