elm elm - 4 months ago 17
Scala Question

Scala extract value from Future[Seq[T]] into Future[T]

For a given

Seq
wrapped in a
Future
, namely for instance

import scala.concurrent._
import ExecutionContext.Implicits.global

val xs = Future { Seq(1,2,3) }


how to extract the first (one only) value in the collection into another
Future
, namely

Future { 1 }

Answer
val xs = Future { Seq(1,2,3) }
val x = xs.map(_.head)

x will be another Future, and xs is still usable, so you can do anything with it.