Re:Why Go Is An Excellent Modern Programming Language
by happy_dino on Tuesday July 30, 2013 @11:40AM
(#6127657)
// Some example methods which might spend waiting for other
// services (calling the database, sending emails).
def getEMailForUsername(username: String) =
future { DB.Users.find(_.name == username).get.email }
def sendPassordRecoveryMail(email: EMail, message: String) =
future { SMTPService.send(email, message) }
// Future/Promises API:
def resetPassword(username: String) =
for {
email <- getEMailForUsername(username)
result <- sendPassordRecoveryMail(email, "some message")
} yield result
// Async API:
def resetPassword(username: String) =
async {
val email = getEMailForUsername(username)
val result = sendPassordRecoveryMail(email, "some message")
await(result)
}
@smegel:
I don't see any “callback hell” or “code that does look like spaghetti” here.
By the way, I'd love to see a link to “Scala's synchronous API” and that mystical “far less attractive callback based API”. Or are you making things up again?
https://news.ycombinator.com/item?id=6125567
http://jim-mcbeath.blogspot.com/2010/08/delimited-continuati...
https://github.com/jimmc/scoroutine - some of the examples in this are godawful, but you'll find that they still work if you rewrite them to be legible.