mattisonchao edited a comment on pull request #13880:
URL: https://github.com/apache/pulsar/pull/13880#issuecomment-1022050128
Hi, @liudezhi2098
I think we need to refactor this method to make it more clear. some
suggestions :
- It is recommended to use "ThenCompose" to compose asynchronous methods and
use the outermost "exceptionally" to handle exceptions. Because a lot of
``catch`` and ``exceptionally`` look like callback hell and hard to read.
- Checked the "catch" logic, it seems that there are places where "catch" is
not needed.
E.g.
**Bad version:**
```java
CompletableFuture.completedFuture(null)
.thenAccept(__ -> {
CompletableFuture.completedFuture(null)
.thenAccept(unused2 -> {
CompletableFuture.completedFuture(null)
.thenAccept(unused3 -> {
}).exceptionally(ex -> {
// handle exception
return null;
});
}).exceptionally(ex -> {
//handle exception
return null;
});
}).exceptionally(ex -> {
//handle exception
return null;
});
```
**Good version:**
```java
CompletableFuture.completedFuture(null)
.thenCompose(__ -> CompletableFuture.completedFuture(null)
.thenCompose(unused2 ->
CompletableFuture.completedFuture(null)
.thenCompose(unused3 -> {
// do somethings
return null;
})
)
).exceptionally(ex -> {
//handle exception
return null;
});
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]