Skip to content

Instantly share code, notes, and snippets.

@joshcough
Created July 19, 2015 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshcough/6ac517c6b972f004816a to your computer and use it in GitHub Desktop.
Save joshcough/6ac517c6b972f004816a to your computer and use it in GitHub Desktop.
def asyncTask(i: I)(implicit e: Executor): Task[O] = Task.suspend({
Task.async { (cb: (Throwable \/ O) => Unit) =>
Futures.addCallback(eval(i), new FutureCallback[O]() {
def onSuccess(result: O) = {
cb(result.right)
self.onSuccess(result)
}
def onFailure(t: Throwable) = {
cb(t.left)
self.onFailure(t)
}
}, e)
}
})
def syncTask(i: I)(implicit e: Executor): Task[O] = Task.suspend({
val fo = eval(i)
Futures.addCallback(fo, new FutureCallback[O]() {
def onSuccess(result: O) = self.onSuccess(result)
def onFailure(t: Throwable) = self.onFailure(t)
}, e)
Task(fo.get)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment