How to unpack Option in Future?

John Doe

I want to implement a method which creates a user if it does not exist yet. If it does exist, the user should be returned.

Here's my code:

def createUserIfNotExists(user: User) = {
for {
  count <- userService.count(Some(user))
  user <- if (count == 0) createUser(user) else userService.findOneByName(user.name)
} yield user
}

My problem is that findOneByName returns Future[Option[User]] and createUser returns Future[User] so the types don't match.

How can I unpack Future[Option[User]] to Future[User] or throw an exception in case there is None?

pamu

make createUser also return Future[Option[_]]

def createUserIfNotExists(user: User) = {
  for {
   count <- userService.count(Some(user))
   userOpt <- if (count == 0) createUser(user).map(Some(_)) else userService.findOneByName(user.name)
  } yield userOpt
}

or

You can do .get on the option because if count is not zero that means user is definitely available in the database.

def createUserIfNotExists(user: User) = {
  for {
   count <- userService.count(Some(user))
   user <- if (count == 0) createUser(user) else userService.findOneByName(user.name).map(_.get)
  } yield user
}

or

First try to retrieve the user if not create the user

def createUserIfNotExists(user: User) = {
 for {
   userOpt <- userService.findOneByName(user.name)
   user <-  userOpt match {
      case Some(value) => value
      case None => createUser(user).map(_ => user)
   }
 } yield user
}

Note that to ensure correctness in case of parallel database operations it is highly recommended to execute the above code in a transaction

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In Scala, how to do type conversion for Future[Option[A]]?

From Dev

How to handle a future option inside of a playframework controller?

From Dev

How should I join Option[Future[T]]?

From Dev

How to bring nested Future out of Option?

From Dev

Future[Option[Future[Int]]] to Future[Option[Int]]

From Dev

Option[Future[Option[Int]]] => Future[Option[Int]]

From Dev

Scalaz: How to Chain Future[List[A]], Option[A] and Future[Option[A]] And Report Individual Error

From Dev

How to apply function of type A => Future[Try[Option[B]]] on given Future[Try[Option[A]]]?

From Dev

How to flatten a Future in scala - "val _2flat: Future[Option[Future[List[Long]]]]

From Dev

`Future[Option[Future[Option[X]]]]` into `Future[Option[X]]`

From Dev

Transform `Future[Option[X]]` into `Option[Future[X]]`

From Dev

Scala Option[Future[T]] to Future[Option[T]]

From Dev

How to Marshall a Future[Option[Foo]] class to JSON in AKKA-HTTP

From Dev

Scala Future with Option()

From Dev

flatten the Option part of a Future[Option[Option[T]]]

From Dev

convert Future[MyType] into Future[Option[MyType]]

From Dev

How to fix this unpack issue?

From Dev

How to unpack a msgpack file?

From Java

How to unpack an .asar file?

From Java

How to unpack pkl file?

From Dev

How to unpack mnist dataset?

From Dev

how to unpack array into variables

From Dev

How to unpack this list comprehension

From Dev

how to unpack the list of elements

From Dev

How to unpack mnist dataset?

From Dev

How to unpack and print a tuple

From Dev

flattening future of option after mapping with a function that return future of option

From Dev

Scala for comprehension with Future, List and Option

From Dev

Understanding the Future[Option[T]] in reactiveMongo

Related Related

  1. 1

    In Scala, how to do type conversion for Future[Option[A]]?

  2. 2

    How to handle a future option inside of a playframework controller?

  3. 3

    How should I join Option[Future[T]]?

  4. 4

    How to bring nested Future out of Option?

  5. 5

    Future[Option[Future[Int]]] to Future[Option[Int]]

  6. 6

    Option[Future[Option[Int]]] => Future[Option[Int]]

  7. 7

    Scalaz: How to Chain Future[List[A]], Option[A] and Future[Option[A]] And Report Individual Error

  8. 8

    How to apply function of type A => Future[Try[Option[B]]] on given Future[Try[Option[A]]]?

  9. 9

    How to flatten a Future in scala - "val _2flat: Future[Option[Future[List[Long]]]]

  10. 10

    `Future[Option[Future[Option[X]]]]` into `Future[Option[X]]`

  11. 11

    Transform `Future[Option[X]]` into `Option[Future[X]]`

  12. 12

    Scala Option[Future[T]] to Future[Option[T]]

  13. 13

    How to Marshall a Future[Option[Foo]] class to JSON in AKKA-HTTP

  14. 14

    Scala Future with Option()

  15. 15

    flatten the Option part of a Future[Option[Option[T]]]

  16. 16

    convert Future[MyType] into Future[Option[MyType]]

  17. 17

    How to fix this unpack issue?

  18. 18

    How to unpack a msgpack file?

  19. 19

    How to unpack an .asar file?

  20. 20

    How to unpack pkl file?

  21. 21

    How to unpack mnist dataset?

  22. 22

    how to unpack array into variables

  23. 23

    How to unpack this list comprehension

  24. 24

    how to unpack the list of elements

  25. 25

    How to unpack mnist dataset?

  26. 26

    How to unpack and print a tuple

  27. 27

    flattening future of option after mapping with a function that return future of option

  28. 28

    Scala for comprehension with Future, List and Option

  29. 29

    Understanding the Future[Option[T]] in reactiveMongo

HotTag

Archive