How to use Future[Option] in Scala

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use Future[Option] in Scala

gl_am_scala
Hi,

  I have a scala application which uses the Slick 3.x for the DB calls. Since all the Slick 3 calls are Async, it returns the Future[Option].

  So I am trying to get the result from a DB call and use that data to do other things. i.e
  
  My DB class has the following code:
  
      // Method to get the User record by username
    def getUserByUsername(username: String): Future[Option[User]] = {
        val query = userData.filter(user => user.username === username)
        val action = query.result.headOption
        db.run(action)
    }
   In my controller, I call the above method and i wanted to use the returned 'User' data for other calls. Can someone please help me how do I do this?


Thanks

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to use Future[Option] in Scala

Vlad Patryshev
The call does not return a User. If you want to apply functions to the value(s) inside, the right solution would be either to use Scala loops, or to use map method, every monad (e.g. Future, Option) has it.

Thanks,
-Vlad

On Thu, Jan 19, 2017 at 1:18 PM, Anil Mathew <[hidden email]> wrote:
Hi,

  I have a scala application which uses the Slick 3.x for the DB calls. Since all the Slick 3 calls are Async, it returns the Future[Option].

  So I am trying to get the result from a DB call and use that data to do other things. i.e
  
  My DB class has the following code:
  
      // Method to get the User record by username
    def getUserByUsername(username: String): Future[Option[User]] = {
        val query = userData.filter(user => user.username === username)
        val action = query.result.headOption
        db.run(action)
    }
   In my controller, I call the above method and i wanted to use the returned 'User' data for other calls. Can someone please help me how do I do this?


Thanks

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to use Future[Option] in Scala

Udas
In reply to this post by gl_am_scala
Vlad is correct. Perhaps you can use the below code as a starting point (copy paste into scala console and play with it).

import scala.concurrent._
import ExecutionContext.Implicits.global
def getUserByUserName(n: String): Future[Option[String]] = Future.successful(Some(n))

def processUser(username: String): Future[Unit] = {
   for {
     optionalUser <- getUserByUserName(username)
     _ = optionalUser map { user =>
        print(s"User is here: ${user}\n")
        // Do whatever you need to do, but say you call a function that is Unit
     } getOrElse {
        print(s"User not found for ${username}\n")
     }
  } yield()
}

val f = processUser("a")
f.value


Hope that helps,
Udas


On Friday, January 20, 2017 at 8:44:56 AM UTC-8, Anil Mathew wrote:
Hi,

  I have a scala application which uses the Slick 3.x for the DB calls. Since all the Slick 3 calls are Async, it returns the Future[Option].

  So I am trying to get the result from a DB call and use that data to do other things. i.e
  
  My DB class has the following code:
  
      // Method to get the User record by username
    def getUserByUsername(username: String): Future[Option[User]] = {
        val query = userData.filter(user => user.username === username)
        val action = query.result.headOption
        db.run(action)
    }
   In my controller, I call the above method and i wanted to use the returned 'User' data for other calls. Can someone please help me how do I do this?


Thanks

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to use Future[Option] in Scala

Lanny Ripple
In reply to this post by gl_am_scala
When I work with Play and Databases I really like the technique described by Erik Bakker here

Options in Futures, how to unsuck them
 https://www.youtube.com/watch?v=hGMndafDcc8


On Friday, January 20, 2017 at 10:44:56 AM UTC-6, Anil Mathew wrote:
Hi,

  I have a scala application which uses the Slick 3.x for the DB calls. Since all the Slick 3 calls are Async, it returns the Future[Option].

  So I am trying to get the result from a DB call and use that data to do other things. i.e
  
  My DB class has the following code:
  
      // Method to get the User record by username
    def getUserByUsername(username: String): Future[Option[User]] = {
        val query = userData.filter(user => user.username === username)
        val action = query.result.headOption
        db.run(action)
    }
   In my controller, I call the above method and i wanted to use the returned 'User' data for other calls. Can someone please help me how do I do this?


Thanks

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.