How do you face nested callbacks in Android? For example, in my app I use Locations API and then, when I have the current lat-lng, I do an HTTP request to my server. In this situation I have two nested callbacks. It's no so bad, but what If I had three or more? I've read about it in this question but I wonder if there are something like Promises for Android.
All I've found is this. Someone knows more about the subject?
Promises are the foundation of asynchronous programming in modern JavaScript. A promise is an object returned by an asynchronous function, which represents the current state of the operation.
Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.
Promise is a future like object that is used as a placeholder for a result of an asynchronous API. Java Future is a synchronization construct that is used to block a thread that called get() method if result is not available yet. Promise differs from it as it cannot be used for blocking.
Promise objects are a means to handle these problems asynchronously with an efficient API. Previously in JavaScript we used callbacks to handle asynchronous tasks however callbacks had many problems when it came to handling async tasks that depend on each other and hence promises was born.
There is something pretty similar already available as part of the Java language, and supported by Android: java.util.concurrent.Future. Perhaps it is good enough for your needs.
By the way, Java 8, which Android does not yet support, has a variant called CompletableFuture that is even closer to a Promise.
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