Skip to content

Commit c09b10e

Browse files
committed
=promise Optimize toCompletableFuture implantation if it's already completed.
1 parent 424b198 commit c09b10e

File tree

1 file changed

+15
-9
lines changed
  • subprojects/parseq/src/main/java/com/linkedin/parseq/promise

1 file changed

+15
-9
lines changed

subprojects/parseq/src/main/java/com/linkedin/parseq/promise/Promise.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,21 @@ public interface Promise<P> {
136136
* @return CompletionStage
137137
*/
138138
default CompletionStage<P> toCompletionStage() {
139-
final CompletableFuture<P> future = new CompletableFuture<>();
140-
addListener(p -> {
141-
if (!p.isFailed()) {
142-
future.complete(p.get());
139+
final CompletableFuture<P> future = new CompletableFuture<>();
140+
if (isDone()) {
141+
future.complete(get());
142+
} else if (isFailed()) {
143+
future.completeExceptionally(getError());
144+
return future;
145+
} else {
146+
addListener(p -> {
147+
if (!p.isFailed()) {
148+
future.complete(p.get());
149+
} else {
150+
future.completeExceptionally(p.getError());
151+
}
152+
});
143153
}
144-
else {
145-
future.completeExceptionally(p.getError());
146-
}
147-
});
148-
return future;
154+
return future;
149155
}
150156
}

0 commit comments

Comments
 (0)