Search code examples
scalaignite

Convert IgniteFuture to Scala Future


It is possible to convert an IgniteFuture to a Scala Future?

I have written the following code:

class ScalaIgniteFuture[T](future: IgniteFuture[T]) {
  def toFuture: Future[T] = {
    val result = Promise[T]
    future
      .listen(f => {
        result.tryComplete(Try{
          f.get()
        })
      })

    result.future
  }
}

It is correct?


Solution

  • Yes, but you can use implicit class pattern to extend the API.

    import scala.util.Try
    
    implicit class IgniteFutureUtils[T](igniteFuture: IgniteFuture[T]) {
     def toScalaFuture = {
       val promise = Promise[T]()
       igniteFuture.listen { k =>
         promise.tryComplete(Try(k.get))
       }
       promise.future
     } 
    }