Imagine having to build functions with names for each while, if/else and foreach statements. Because that's what it feels like when working with async I/O in Python, a complete pain in the ass compared to other languages.
Scala sample:
cache.get[String]("name").flatMap {
case Some(value) => value
case None =>
database.query("names").head.flatMap {
case Some(id, value) =>
cache.set("name", value, 10.minutes)
.map(_ => value)
case None =>
Future.successful("Anonymous")
}
}
BTW, in this sample, for comprehensions are not that useful. But if you're using Scala-Async, you can write that in a style resembling blocking I/O:
async {
val cached = await(cache.get[String]("name"))
cached match {
case Some(value) => value
case None =>
val fromDB = await(database.query("names").head)
fromDB match {
case Some(id, value) =>
await(cache.set("name", value, 10.minutes))
value
case None =>
"Anonymous"
}
}
}
In both cases multi-line anonymous functions are leveraged.