#Java's CompletableFuture
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
CompletableFuture completableFuture = CompletableFuture...;
completableFuture.thenAccept(result -> System.out.println(result)); ```
No i mean like javascript's await
completablefuture.get();
might want to be careful next time, u tagged this thread with Java, not javascript
Changed the category to Other.
<@&987246964494204979> please have a look, thanks.
No
read again
i wrote like in js
sorry, what?
He wants to know what is the equivalent of await in java with CompletableFuture from what I understand
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()
then he should ask it like this next time :D
Changed the category to Java.
<@&987246399047479336> please have a look, thanks.
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
@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
Why get() and not join()?
.get will get you the value
and join?
join will wait until the value is available
actually join seems good
it also gives the value
the difference is, that get throws a checked exception and join doesnt but the functionality is the same