Skip to content

Commit

Permalink
Added RestAction#onSuccess (#2227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zabuzard authored Sep 20, 2022
1 parent 0917474 commit 567693e
Showing 1 changed file with 40 additions and 0 deletions.
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
*/
@Nonnull
@CheckReturnValue
default RestAction<T> onSuccess(@Nonnull Consumer<? super T> consumer)
{
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

0 comments on commit 567693e

Please sign in to comment.