Reputation: 69
My scenario is, I have multiple API's call to be made and each of them will require to use data from previous call.
Now let's suppose I made a API call with future, and return response of the first call, I am not able to call next API from within Oncomplete of first call.
Upvotes: 0
Views: 404
Reputation: 28511
You could use flatMap
or the cleaner alternative to sequencing a bunch of of futures, which is to use the for yield
syntax. It's just compile time sugar that will internally transform itself into flatMap
and map
and withFilter
method calls, but it's generally cleaner and easier to read.
import scala.concurrent.ExecutionContext.Implicits.global
for {
first <- firstApiCall()
second <- secondApiCall(first)
} yield second
Upvotes: 3
Reputation: 7735
You can link Futures with flatMap function
def firstApiCall(): Future[FirstRes]
def secondApiCall(firstRes: FirstRes): Future[SecondRes]
def combinedCall()(implicit ec: ExecutionContext): Future[SecondRes] = firstApiCall.flatMap(res => secondApiCall(res))
Upvotes: 5