Skip to content

Instantly share code, notes, and snippets.

@satyagraha
Created September 28, 2013 13:12
Show Gist options
  • Save satyagraha/6741946 to your computer and use it in GitHub Desktop.
Save satyagraha/6741946 to your computer and use it in GitHub Desktop.
Why does this app seem to block at line 42?
package org.satyagraga.dispatch.evaluation
import dispatch._, Defaults._
import org.json4s._
import org.json4s.native.JsonMethods._
case class ITune(trackId: Int, trackName: String)
object ITunesApp {
private implicit val formats = org.json4s.DefaultFormats
val itunesUrl = "http://itunes.apple.com/search?term=metallica"
val itunesApi = url(itunesUrl)
def callITunes(): Future[Either[Throwable, String]] = {
println("callITunes - enter")
val res = Http(itunesApi OK as.String).either
println("callITunes - exit")
res
}
def extractITunes(itunesJsonData: String): List[ITune] = {
println("extractitunes - enter")
val res = for (ituneJson <- (parse(itunesJsonData) \ "results").children)
yield ituneJson.extract[ITune]
println("extractitunes - exit: " + res)
res
}
def fetchAllITunes(): Future[Either[Throwable, List[ITune]]] = {
println("fetchAllitunes - enter")
val req = callITunes()
val res = req.map(resp =>
{
println("fetchAllitunes - callback - resp")
val ituneData = resp match {
case Left(throwable) => Left(throwable)
case Right(itunesJsonData) => {
println("fetchAllitunes - callback - itunesJsonData: " + itunesJsonData.take(100) + " ...")
val itunes = extractITunes(itunesJsonData)
println("fetchAllitunes - callback -itune list: " + itunes)
Right(itunes)
}
}
println("fetchAllitunes - callback - ituneData: " + ituneData)
ituneData
})
println("fetchAllitunes - exit - res: " + res)
res
}
println("fetching allITunes")
val allITunes = fetchAllITunes().apply
println("allITunes: " + allITunes)
allITunes match {
case Right(itunes) => println("itunes: " + itunes)
case Left(throwable) => println("throwable: " + throwable)
}
println("allITunes: " + allITunes)
def main(args: Array[String]) {
println("Running...")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment