You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Promise represents a proxy for a value not necessarily known when the promise is created. It
16
-
allows you to associate dependent promises to an asynchronous action's eventual success value or
17
-
failure reason. Promises are a way to write async code that still appears as though it is executing
18
-
in a synchronous way.
20
+
The Promise design pattern is used to handle asynchronous operations by providing a placeholder for a result that is initially unknown but will be resolved in the future.
19
21
20
22
## Explanation
21
23
22
-
The Promise object is used for asynchronous computations. A Promise represents an operation that
23
-
hasn't completed yet, but is expected in the future.
24
+
Real-world example
24
25
25
-
Promises provide a few advantages over callback objects:
26
-
* Functional composition and error handling.
27
-
* Prevents callback hell and provides callback aggregation.
28
-
29
-
Real world example
30
-
31
-
> We are developing a software solution that downloads files and calculates the number of lines and
32
-
> character frequencies in those files. Promise is an ideal solution to make the code concise and
33
-
> easy to understand.
26
+
> In an online pizza ordering system, when a customer places an order, the system immediately acknowledges the order and provides a tracking number (the promise). The pizza preparation and delivery process happens asynchronously in the background. The customer can check the status of their order at any time using the tracking number. Once the pizza is prepared and out for delivery, the customer receives a notification (promise resolved) about the delivery status. If there are any issues, such as an unavailable ingredient or delivery delay, the customer is notified about the error (promise rejected).
27
+
>
28
+
> This analogy illustrates how the Promise design pattern manages asynchronous tasks, decoupling the initial request from the eventual outcome, and handling both results and errors efficiently.
34
29
35
30
In plain words
36
31
37
32
> Promise is a placeholder for an asynchronous operation that is ongoing.
38
33
39
34
Wikipedia says
40
35
41
-
> In computer science, future, promise, delay, and deferred refer to constructs used for
42
-
> synchronizing program execution in some concurrent programming languages. They describe an object
43
-
> that acts as a proxy for a result that is initially unknown, usually because the computation of
44
-
> its value is not yet complete.
36
+
> In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown, usually because the computation of its value is not yet complete.
45
37
46
38
**Programmatic Example**
47
39
48
-
In the example a file is downloaded and its line count is calculated. The calculated line count is
49
-
then consumed and printed on console.
40
+
The Promise design pattern is a software design pattern that's often used in concurrent programming to handle asynchronous operations. It represents a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason.
50
41
51
-
Let's first introduce a support class we need for implementation. Here's `PromiseSupport`.
42
+
In the provided code, the Promise design pattern is used to handle various asynchronous operations such as downloading a file, counting lines in a file, and calculating the character frequency in a file.
In this code, the `Promise` class is used to create promises for various operations. The `thenApply` method is used to chain promises, meaning that the result of one promise is used as the input for the next promise. The `thenAccept` method is used to handle the result of a promise. The `fulfillInAsync` method is used to fulfill a promise asynchronously, and the `onError` method is used to handle any errors that occur while fulfilling the promise.
115
+
278
116
## Class diagram
279
117
280
-

118
+

281
119
282
120
## Applicability
283
121
284
-
Promise pattern is applicable in concurrent programming when some work needs to be done
285
-
asynchronously and:
122
+
* When you need to perform asynchronous tasks and handle their results or errors at a later point.
123
+
* In scenarios where tasks can be executed in parallel and their outcomes need to be handled once they are completed.
124
+
* Suitable for improving the readability and maintainability of asynchronous code.
286
125
287
-
* Code maintainability and readability suffers due to callback hell.
288
-
* You need to compose promises and need better error handling for asynchronous tasks.
289
-
* You want to use functional style of programming.
126
+
## Tutorials
290
127
128
+
*[Guide To CompletableFuture](https://www.baeldung.com/java-completablefuture)
* Improved Readability: Simplifies complex asynchronous code, making it easier to understand and maintain.
142
+
* Decoupling: Decouples the code that initiates the asynchronous operation from the code that processes the result.
143
+
* Error Handling: Provides a unified way to handle both results and errors from asynchronous operations.
303
144
304
-
*[Guide To CompletableFuture](https://www.baeldung.com/java-completablefuture)
145
+
Trade-offs:
146
+
147
+
* Complexity: Can add complexity to the codebase if overused or misused.
148
+
* Debugging: Asynchronous code can be harder to debug compared to synchronous code due to the non-linear flow of execution.
149
+
150
+
## Related Patterns
151
+
152
+
*[Observer](https://java-design-patterns.com/patterns/observer/): Promises can be used in conjunction with the Observer pattern to notify subscribers about the completion of asynchronous operations.
153
+
*[Callback](https://java-design-patterns.com/patterns/callback/): Promises often replace callback mechanisms by providing a more structured and readable way to handle asynchronous results.
154
+
*[Async Method Invocation](https://java-design-patterns.com/patterns/async-method-invocation/): Promises are often used to handle the results of asynchronous method invocations, allowing for non-blocking execution and result handling.
305
155
306
156
## Credits
307
157
308
158
*[You are missing the point to Promises](https://gist.github.com/domenic/3889970)
309
159
*[Functional style callbacks using CompletableFuture](https://www.infoq.com/articles/Functional-Style-Callbacks-Using-CompletableFuture)
310
160
*[Java 8 in Action: Lambdas, Streams, and functional-style programming](https://www.amazon.com/gp/product/1617291994/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617291994&linkId=995af46887bb7b65e6c788a23eaf7146)
311
161
*[Modern Java in Action: Lambdas, streams, functional and reactive programming](https://www.amazon.com/gp/product/1617293563/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617293563&linkId=f70fe0d3e1efaff89554a6479c53759c)
162
+
*[Java Concurrency in Practice](https://amzn.to/4aRMruW)
163
+
*[Effective Java](https://amzn.to/4cGk2Jz)
164
+
*[Java 8 in Action: Lambdas, Streams, and functional-style programming](https://amzn.to/3QCmGXs)
0 commit comments