asynchronous - Java play framework Async operation on same object -


i'm trying perform async operations on same object example pass list promise perform concurrently 3 queries each query when finishes adds queried results list , http result returns full list when concurrent queries results finish.

having @ tutorial:

https://www.playframework.com/documentation/2.2.x/javaasync

i make

    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 , neither pass object promise , neither can result of both queries handle both results.

you can use promise.sequence achieve that

f.promise<integer> one, two; 1 = f.promise.promise(new f.function0<integer>() {     @override public integer apply() throws throwable {         return 20;     } }); 2 = 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 " + res);     } }); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -