Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promises for Android?

Tags:

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?

like image 786
Marcos Avatar asked Apr 19 '14 00:04

Marcos


People also ask

What are programming promises?

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.

What is the use of promise?

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.

What is the promise of Java?

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.

What is promise in Web API?

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.


1 Answers

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.

like image 166
cybersam Avatar answered Nov 03 '22 05:11

cybersam