@@ -14,38 +14,41 @@ CompletableFuture
14
14
15
15
## Intent
16
16
17
- A Promise represents a proxy for a value not necessarily known when the promise is created. It allows you to associate
18
- dependent promises to an asynchronous action's eventual success value or failure reason. Promises are a way to write
19
- async code that still appears as though it is executing in a synchronous way.
17
+ A Promise represents a proxy for a value not necessarily known when the promise is created. It
18
+ allows you to associate dependent promises to an asynchronous action's eventual success value or
19
+ failure reason. Promises are a way to write async code that still appears as though it is executing
20
+ in a synchronous way.
20
21
21
22
## Explanation
22
23
23
- The Promise object is used for asynchronous computations. A Promise represents an operation that hasn't completed yet,
24
- but is expected in the future.
24
+ The Promise object is used for asynchronous computations. A Promise represents an operation that
25
+ hasn't completed yet, but is expected in the future.
25
26
26
27
Promises provide a few advantages over callback objects:
27
- * Functional composition and error handling
28
- * Prevents callback hell and provides callback aggregation
28
+ * Functional composition and error handling.
29
+ * Prevents callback hell and provides callback aggregation.
29
30
30
31
Real world example
31
32
32
- > We are developing a software solution that downloads files and calculates the number of lines and character
33
- frequencies in those files. Promise is an ideal solution to make the code concise and easy to understand.
33
+ > We are developing a software solution that downloads files and calculates the number of lines and
34
+ > character frequencies in those files. Promise is an ideal solution to make the code concise and
35
+ > easy to understand.
34
36
35
37
In plain words
36
38
37
39
> Promise is a placeholder for an asynchronous operation that is ongoing.
38
40
39
41
Wikipedia says
40
42
41
- > In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program
42
- execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is
43
- initially unknown, usually because the computation of its value is not yet complete.
43
+ > In computer science, future, promise, delay, and deferred refer to constructs used for
44
+ > synchronizing program execution in some concurrent programming languages. They describe an object
45
+ > that acts as a proxy for a result that is initially unknown, usually because the computation of
46
+ > its value is not yet complete.
44
47
45
48
** Programmatic Example**
46
49
47
- In the example a file is downloaded and its line count is calculated. The calculated line count is then consumed and
48
- printed on console.
50
+ In the example a file is downloaded and its line count is calculated. The calculated line count is
51
+ then consumed and printed on console.
49
52
50
53
Let's first introduce a support class we need for implementation. Here's ` PromiseSupport ` .
51
54
@@ -195,7 +198,7 @@ public class Promise<T> extends PromiseSupport<T> {
195
198
196
199
public <V > Promise<V > thenApply (Function<? super T , V > func ) {
197
200
Promise<V > dest = new Promise<> ();
198
- fulfillmentAction = new TransformAction<V > (this , dest, func);
201
+ fulfillmentAction = new TransformAction<> (this , dest, func);
199
202
return dest;
200
203
}
201
204
@@ -246,8 +249,8 @@ public class Promise<T> extends PromiseSupport<T> {
246
249
}
247
250
```
248
251
249
- Now we can show the full example in action. Here's how to download and count the number of lines in a file using
250
- ` Promise ` .
252
+ Now we can show the full example in action. Here's how to download and count the number of lines in
253
+ a file using ` Promise ` .
251
254
252
255
``` java
253
256
countLines(). thenAccept(
@@ -280,8 +283,8 @@ Now we can show the full example in action. Here's how to download and count the
280
283
281
284
## Applicability
282
285
283
- Promise pattern is applicable in concurrent programming when some work needs to be done asynchronously
284
- and:
286
+ Promise pattern is applicable in concurrent programming when some work needs to be done
287
+ asynchronously and:
285
288
286
289
* Code maintainability and readability suffers due to callback hell.
287
290
* You need to compose promises and need better error handling for asynchronous tasks.
0 commit comments