#Java's CompletableFuture

1 messages · Page 1 of 1 (latest)

gray grove
#

How can I use the code of .thenAccept in the same line without idention

digital trellisBOT
#

<@&987246399047479336> please have a look, thanks.

digital trellisBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

wooden light
#
CompletableFuture completableFuture = CompletableFuture...;
completableFuture.thenAccept(result -> System.out.println(result));  ```
gray grove
wooden light
#

completablefuture.get();

ionic nymph
digital trellisBOT
#

Changed the category to Other.

#

<@&987246964494204979> please have a look, thanks.

gray grove
#

read again

#

i wrote like in js

ionic nymph
#

sorry, what?

smoky hearth
ionic nymph
#

are u talking about java? then look at wazeis snippet

#

if js, then its sth entirely different

#

javas future api is different to js. u would work inside the future chain instead

#

don't use join()

sturdy sinew
digital trellisBOT
#

Changed the category to Java.

#

<@&987246399047479336> please have a look, thanks.

ionic nymph
#

Also, thenAccept doesn't produce a result

#

its for consuming, not transforming

#

thenApply produces a result

#

join() and get() can be used to get hands on the result and wait. but generally, if u use them, ur likely doing sth wrong

#

instead, continue in the chain

#

foo.thenApply(...).thenApply(...).thenApply(...).thenAccept(...)

#

we would need to know more details about the actual use case to give more concrete feedback

lapis obsidian
#

@gray grove I keep saying this, but maybe I need to be more clear

#

in Javascript, you need async/await because intrinsically the runtime is "single threaded"

#

so "blocking the main thread" is a big deal

#

in Java, that is not the case

#

both today (for 90% of things) and tommorow (for the last achievable 9.9%) its actually fine to just make blocking calls

#

there is no "main thread" usually. You can make as many threads as you need

#

what CompletableFuture is good for is the equivalent of Promise.all

#

running some tasks in parallel

#

this is a very deep topic

#

but the advice for you, practically, is just to make the blocking call

#

maybe useful if you need to do stuff in parallel

#

The library you seem to be using

#
CompletableFuture<List<HashMap<String, Object>>> banFuture = new CompletableFuture<>();
guild.retrieveBanList().queue(bans -> {
    //dostuff
    banFuture.complete(banList);
}, banFuture::completeExceptionally);

CompletableFuture<List<HashMap<String, Object>>> emojiFuture = new CompletableFuture<>();
guild.retrieveEmojis().queue(emojis -> {
    emojiFuture.complete(emojiList);
}, emojiFuture::completeExceptionally);
#

seems to want to use async apis (providing you data in callbacks)

#

but once you got it out of the callback - use .get()

#

if you could give a more complete code sample

#

ideally something I can run on my own machine

#

then i can give more concrete advice

gray grove
lapis obsidian
gray grove
lapis obsidian
#

join will wait until the value is available

#

actually join seems good

#

it also gives the value

fallow hatch
#

the difference is, that get throws a checked exception and join doesnt but the functionality is the same