Skip to content

Commit 67e1c11

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

File tree

1 file changed

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

1 file changed

+14
-9
lines changed

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,20 @@ 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+
} else {
145+
addListener(p -> {
146+
if (!p.isFailed()) {
147+
future.complete(p.get());
148+
} else {
149+
future.completeExceptionally(p.getError());
150+
}
151+
});
143152
}
144-
else {
145-
future.completeExceptionally(p.getError());
146-
}
147-
});
148-
return future;
153+
return future;
149154
}
150155
}

0 commit comments

Comments
 (0)