Reputation: 1970
I'm trying to perform Async operations on the same object for example I would pass List to Promise to perform concurrently 3 queries where each query when finishes adds queried results to the List and finally the http result returns the Full list when all concurrent queries results finish.
Having a look at this tutorial:
https://www.playframework.com/documentation/2.2.x/JavaAsync
I could make something like this
return async(
promise(new Function0<Integer>() {
public Integer apply() {
firstQuery();
}
})
. promise(new Function0<Integer>() {
public Integer apply() {
return secondQuery();
}
})
.map(new Function<Integer,Result>() {
public Result apply(Integer i) {
Logger.debug("we have got "+i);
return ok("Got " + i);
}
})
);
to make 2 operations work concurrently , but neither I could pass an object to promise and neither I can get the result of both queries to handle both results.
Upvotes: 1
Views: 124
Reputation: 12986
You can use Promise.sequence to achieve that
F.Promise<Integer> one, two;
one = F.Promise.promise(new F.Function0<Integer>() {
@Override public Integer apply() throws Throwable {
return 20;
}
});
two = F.Promise.promise(new F.Function0<Integer>() {
@Override public Integer apply() throws Throwable {
return 22;
}
});
return F.Promise.sequence(one, two).map(new F.Function<List<Integer>, Result>() {
@Override
public Result apply(List<Integer> integers) throws Throwable {
int res = integers.get(0) + integers.get(1);
return ok("The answer is " + res);
}
});
Upvotes: 1