Created
November 26, 2013 09:49
-
-
Save mdread/7655832 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import org.elasticsearch.action.ActionRequestBuilder | |
import org.elasticsearch.action.ActionResponse | |
import org.elasticsearch.action.ActionListener | |
import scala.concurrent.{ Promise, Future } | |
import scala.util.Try | |
object Futurastic { | |
def execute[T, R <: ActionResponse](builder: ActionRequestBuilder[_, R, _])(action: (R) => Try[T]): Future[T] = { | |
val prom = Promise[T]() | |
builder.execute(new ActionListener[R] { | |
def onResponse(res: R) = prom.complete(action(res)) | |
def onFailure(exception: Throwable) = prom.failure(exception) | |
}) | |
prom.future | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment