score:0

Accepted answer

I tried to do what you want with random number generation.

import scala.concurrent.{Await, Future}
import scala.util.Random

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

val RANDOM = new Random()

def futureRec(num: Int, f: Future[List[Integer]]): Future[List[Integer]] = {
  if(num == 0) {
    f
  } else {
    f.flatMap(l => {
      futureRec(num - 1, Future.successful(RANDOM.nextInt() :: l))
    })
  }
}

val futureResult = futureRec(5, Future.successful(Nil))

Await.result(futureResult, 5 minutes)

So I would do, what you want something like this:

def getData(url: String, userID: String):Future[List[PostMD]] = {
  def inner(url: String, f: Future[List[PostMD]]): Future[List[PostMD]] = {

      val chunk: Future[JsValue]  = ???
      chunk.flatMap(ch => {
        val postMd = (ch \ "data").\\("data").map(_.as[PostMD]).toList
        val relatedPostMd = postMd.filter(_.fromID == userID)
        val next: String = (ch.\("paging").\("next")).as[String]
        if (next != null)
           inner(next, f.map(l => l ++ relatedPostMd))
        else
           f.map(l => l ++ relatedPostMd)
      })
  }

  inner(url, Future.successful(Nil))
}

Related Query

More Query from same tag