Skip to content

Instantly share code, notes, and snippets.

@mdread
Created November 26, 2013 09:49
Show Gist options
  • Save mdread/7655832 to your computer and use it in GitHub Desktop.
Save mdread/7655832 to your computer and use it in GitHub Desktop.
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