Last active
August 29, 2015 14:05
-
-
Save longliveenduro/92974f9aefa801b0810a to your computer and use it in GitHub Desktop.
Future example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fetchUserData(userId: Int): Future[UserData] = {...} // method which returns user data in the future | |
| def loadFromUrl(url: Url): Future[Bitmap] = {...} // method which returns a bitmap in the future | |
| val avatar: Future[Bitmap] = | |
| for { | |
| userData <- fetchUserData(1) // bind user data to value 'userData' as soon as it is available | |
| avatar <- loadFromUrl(userData.avatarUrl) // use 'userData' from above (when it is available) and then | |
| // load the bitmap and bind it to 'avatar' as soon as it is available | |
| } yield avatar // return the avatar bitmap as a result | |
| // the value 'avatar' will contain a bitmap as soon as the the chain the for comprehension is executed | |
| // no blocking of threads will happen at any point ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment