Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RestAction#onSuccess #2227

Merged
merged 4 commits into from
Sep 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/main/java/net/dv8tion/jda/api/requests/RestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,46 @@ default <O> RestAction<O> map(@Nonnull Function<? super T, ? extends O> map)
return new MapRestAction<>(this, map);
}

/**
* An intermediate operator that returns a modified RestAction.
*
* <p>This does not modify this instance but returns a new RestAction, which will consume
* the actions result using the given consumer on successful execution.
* The resulting action continues with the previous result.
*
* <p><b>Example</b><br>
* <pre>{@code
* public RestAction<String> retrieveMemberNickname(Guild guild, String userId) {
* return guild.retrieveMemberById(userId)
* .map(Member::getNickname)
* .onSuccess(System.out::println);
* }
* }</pre>
*
* Prefer using {@link #queue(Consumer)} instead, if continuation of the action
* chain is not desired.
*
* @param consumer
* The consuming function to apply to the action result, failures are propagated
* into the resulting action
*
*
* @throws IllegalArgumentException
* If the consumer is null
*
* @return RestAction that consumes the action result
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
*/
@Nonnull
@CheckReturnValue
default RestAction<T> onSuccess(@Nonnull Consumer<? super T> consumer)
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
{
Checks.notNull(consumer, "Consumer");
return map(result -> {
consumer.accept(result);
return result;
});
}

/**
* Supply a fallback value when the RestAction fails for any reason.
*
Expand Down