handle null in compose methods

This commit is contained in:
WhatCats
2024-04-07 14:30:42 +02:00
parent c2e4e8c522
commit 54d7b02675
2 changed files with 14 additions and 6 deletions

View File

@@ -166,12 +166,16 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
} }
@Override @Override
public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task) { public <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, Promise<V>> task) {
Promise<V> promise = getFactory().unresolved(); Promise<V> promise = getFactory().unresolved();
thenApplySync(task).addDirectListener( thenApplySync(task).addDirectListener(
nestedPromise -> { nestedPromise -> {
propagateResult(nestedPromise, promise); if (nestedPromise == null) {
propagateCancel(promise, nestedPromise); promise.complete(null);
} else {
propagateResult(nestedPromise, promise);
propagateCancel(promise, nestedPromise);
}
}, },
promise::completeExceptionally promise::completeExceptionally
); );
@@ -267,8 +271,12 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
Promise<V> promise = getFactory().unresolved(); Promise<V> promise = getFactory().unresolved();
thenApplyAsync(task).addDirectListener( thenApplyAsync(task).addDirectListener(
nestedPromise -> { nestedPromise -> {
propagateResult(nestedPromise, promise); if (nestedPromise == null) {
propagateCancel(promise, nestedPromise); promise.complete(null);
} else {
propagateResult(nestedPromise, promise);
propagateCancel(promise, nestedPromise);
}
}, },
promise::completeExceptionally promise::completeExceptionally
); );

View File

@@ -33,7 +33,7 @@ public interface Promise<T> {
<V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit); <V> @NotNull Promise<V> thenApplyDelayedSync(@NotNull ExceptionalFunction<T, V> task, long delay, @NotNull TimeUnit unit);
<V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, @NotNull Promise<V>> task); <V> @NotNull Promise<V> thenComposeSync(@NotNull ExceptionalFunction<T, Promise<V>> task);
@NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task); @NotNull Promise<Void> thenRunAsync(@NotNull ExceptionalRunnable task);