I would like to know if there is something similar to the "Promise.all" of JavaScript for Java 8 (currently on Android). I am trying to make when all the callbacks finish, then execute a second process.
Now the callbacks that I am using are lambdas expressions, but I'm open to suggestions to change the way to do the callbacks and the "big callback" that is executed when all callbacks finishes.
I made it with a counter of callbacks, that when that counter of callbacks is equal to the length of callbacks then call to the big callback. But I am sure that this solution is not the correct one.
A Promise is in one of these states: pending: initial state, neither fulfilled nor rejected. fulfilled: meaning that the operation was completed successfully. rejected: meaning that the operation failed.
all() The Promise. all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will fulfill when all of the input's promises have fulfilled, or if the input iterable contains no promises.
Javascript. 2. Async/Await: Async/Await is used to work with promises in asynchronous functions.
JavaScript's Promise. all() method takes in a parameter of iterable promises runs them concurrently then returns a single Promise that resolves to an array of results of the input promises.
Java 8 offers this as CompletableFuture.allOf(CompletableFuture...), which mimics Promise.all; there is also CompletableFuture.anyOf(CompletableFuture...) to mimic Promise.race in Javascript.
If you use ListenableFuture instead, Guava does this as Futures.allAsList(...), with both varargs and iterable overloads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With