Cherry
Cherry

Reputation: 33544

Is there a Future.sequence analog in kotlin?

In scala you can "map" collection of futures to future of collection like that:

val l: List[Future[String]]  = List(Future {"1"}, Future {"2"})
val x: Future[List[String]] = Future.sequence(l)

How to same thing but with kotlin?

Upvotes: 1

Views: 416

Answers (1)

voddan
voddan

Reputation: 33769

Assuming you use coroutines:

val l: List<Deferred<String>>  = (1..2).map {i -> async(Unconfined){ "$i" }}
val x: Deffered<List<String>> = async(Unconfined) { l.map {it.await()} }

Upvotes: 5

Related Questions