-
-
Save bchazalet/d07a836cdd5d79ffbdcb to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Created by arya on 1/2/15. | |
*/ | |
/* | |
scalaVersion := "2.11.4" | |
libraryDependencies += "com.ning" % "async-http-client" % "1.9.3" | |
libraryDependencies += "org.scalaz" %% "scalaz-concurrent" % "7.1.0" | |
*/ | |
package example | |
import com.ning.http.client._ | |
import scalaz._ | |
import scalaz.concurrent.Task | |
trait Version1 { | |
val asyncHttp: AsyncHttpClient | |
asyncHttp.prepareGet("http://google.com").execute(new AsyncCompletionHandler[Unit] { | |
override def onCompleted(r: Response): Unit = ??? | |
override def onThrowable(t: Throwable): Unit = ??? | |
}) | |
} | |
trait Version2 { | |
val asyncHttp: AsyncHttpClient | |
def get(s: String): Task[Response] = | |
Task.async[Response](k => asyncHttp.prepareGet(s).execute(toHandler(k))) | |
trait MyResults | |
def processData: Response => Task[MyResults] | |
def toHandler(k: (Throwable \/ Response) => Unit) = new AsyncCompletionHandler[Unit] { | |
override def onCompleted(r: Response): Unit = k(\/-(r)) | |
override def onThrowable(t: Throwable): Unit = k(-\/(t)) | |
} | |
val myTask: Task[MyResults] = get("http://google.com").flatMap(processData)//...etc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment