diff --git a/src/main/java/net/dv8tion/jda/api/requests/RestAction.java b/src/main/java/net/dv8tion/jda/api/requests/RestAction.java index 3a0feed5ee..337f3f5488 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/RestAction.java +++ b/src/main/java/net/dv8tion/jda/api/requests/RestAction.java @@ -764,6 +764,46 @@ default RestAction map(@Nonnull Function map) return new MapRestAction<>(this, map); } + /** + * An intermediate operator that returns a modified RestAction. + * + *

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. + * + *

Example
+ *

{@code
+     * public RestAction retrieveMemberNickname(Guild guild, String userId) {
+     *     return guild.retrieveMemberById(userId)
+     *                 .map(Member::getNickname)
+     *                 .onSuccess(System.out::println);
+     * }
+     * }
+ * + * 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 onSuccess(@Nonnull Consumer consumer) + { + Checks.notNull(consumer, "Consumer"); + return map(result -> { + consumer.accept(result); + return result; + }); + } + /** * Supply a fallback value when the RestAction fails for any reason. *