I\'d like to create custom tasks like these ones in firebase in order to chain my API async calls. How can I achieve that?
Suppose you have a Document
class, you could do as follow:
Tasks.forResult(document);
Tasks.forException(new RuntimeException("Cool message"));
interface CreateDocument extends Callable {
@Override
Document call();
}
Tasks.call(new CreateDocument());
Task createDocument() {
TaskCompletionSource tcs = new TaskCompletionSource();
if (this.someThingGoesWrong()) {
tcs.setException(new RuntimeException("Cooler message"));
} else {
tcs.setResult(Document.factory());
}
tcs.getTask();
}