Prevents callback hell and provides callback aggregation
*
*
+ *
+ *
* @see CompletableFuture
*/
public class App {
@@ -68,23 +79,57 @@ public class App {
private static void promiseUsage(Executor executor)
throws InterruptedException, ExecutionException {
- Promise consumedPromise = new Promise<>();
- consumedPromise.fulfillInAsync(() -> {
- Thread.sleep(1000);
- return 10;
- }, executor).then(value -> {
- System.out.println("Consumed int value: " + value);
+ String urlString = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md";
+ Promise lineCountPromise = new Promise().fulfillInAsync(() -> {
+ return downloadFile(urlString);
+ }, executor).then(fileLocation -> {
+ return countLines(fileLocation);
});
- Promise transformedPromise = new Promise<>();
- transformedPromise.fulfillInAsync(() -> {
- Thread.sleep(1000);
- return "10";
- }, executor).then(value -> { return Integer.parseInt(value); }).then(value -> {
- System.out.println("Consumed transformed int value: " + value);
+ Promise