In Scala, I can take a list of values, display a function of future return through them, and return a future that will collect the values โโof these futures into a list (or crash with the first error). More specific:
import scala.concurrent._ import scala.concurrent.ExecutionContext.Implicits.global def doSomething(i: Int): Future[Int] = future(i + 1) // Not really doing much. val incremented = Future.traverse(List(1, 2, 3))(doSomething)
In this case, the result will be only an enlarged list:
scala> incremented.onSuccess { case xs => println(xs) } List(2, 3, 4)
I could also create a list of futures and then turn them into a future containing the same result:
val incremented = Future.sequence(List(1, 2, 3).map(doSomething))
This will give me the same thing, but it creates an extra intermediate collection and is a bit noisier.
I want to do something like this with promises in Q , and it looks like Q.all larger or smaller sequence :
function doSomething(i) { return Q.fcall(function () { return i + 1; }); } Q.all([1, 2, 3].map(doSomething)).then(function (xs) { console.log(xs) })
Is there a way to write a more traverse like version? This is such a basic operation that it seems like there should be a way to do this, but this is my first day with Q, and I'm still working on all overloads of fcall and friends.