Skip to content

Instantly share code, notes, and snippets.

@longliveenduro
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save longliveenduro/92974f9aefa801b0810a to your computer and use it in GitHub Desktop.

Select an option

Save longliveenduro/92974f9aefa801b0810a to your computer and use it in GitHub Desktop.
Future example
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