Documentation

Learn how to install and use LiquidBounce with our comprehensive guides

AsyncUtil

The AsyncUtil class offers a set of utilities for handling asynchronous operations using Promises in JavaScript.

All command and module handlers (including onEnable and onDisable) can be written as async function, like in the following examples:

const onEnable = async () => {
  await AsyncUtil.ticks(1);
  Chat.displayChatMessage("Enabled!");
}
module.on("chatReceive", async (event) => {
  const message = event.getMessage();
  await AsyncUtil.ticks(3);
  await AsyncUtil.until(() => mc.player.isOnGround());
  await AsyncUtil.conditional(20, mc.player.isOnGround);
});

Note: Since Promise does not support cancellation, any async function that has already started will continue running even if the module is disabled.

Unlike browsers or Node.js, GraalJS does not support web APIs such as setTimeout, setInterval, or fetch. These are typically available on the Window object object in browsers.

Instead, this utility provides delay and timing functions that work within the game's render thread event loop.

Additionally, AsyncUtil includes support for making HTTP requests using OkHttp, which is widely used in this mod:

const test = "Guten Tag!";
const sourceLanguage = "de";
const targetLanguage = "en";
/** okhttp3.Response */
const response = await AsyncUtil.request((builder) => {
  builder.url(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLanguage}&tl=${targetLanguage}&dt=t&q=${encodeURIComponent(text)}`);
});
const code = response.code(); // No await here!

You can also run tasks asynchronously on a specified ExecutorService, with coroutine-style results:

const result = await AsyncUtil.launch(() => {
  java.lang.Thread.sleep(5000);
  return "task result";
});
Chat.displayChatMessage(result);

Method Summary

All methods resolve or reject their Promises on the render thread.


AsyncUtil.ticks(n: int): Promise<int>

Returns a Promise that resolves after n game ticks. The resolved value is the number of ticks waited.

Parameters:

  • n: Number of ticks to wait. If n <= 0, it immediately resolves with 0.

AsyncUtil.until(condition: () => boolean): Promise<int>

Returns a Promise that resolves when the condition function returns true. The resolved value is the number of ticks waited.

Parameters:

  • condition: A function that is evaluated every tick. When it returns true, the promise resolves.

AsyncUtil.conditional(n: int, breakLoop: () => boolean): Promise<int>

Returns a Promise that resolves when either the breakLoop function returns true, or n ticks have passed. Whichever comes first. The resolved value is the number of ticks waited.

Parameters:

  • n: Maximum number of ticks to wait.
  • breakLoop: A condition function evaluated each tick. If it returns true, the promise resolves early.

AsyncUtil.request(block: (Request.Builder) => void): Promise<Response>

Creates a Promise that performs an HTTP request and resolves with the resulting Response.

Parameters:


AsyncUtil.launch<T>(executor: ExecutorService, block: () => T): Promise<T>

Runs a task asynchronously on a given ExecutorService, returning a Promise that resolves with the result of the task.

Parameters:

  • executor: (Optional) The ExecutorService to run the task on. Defaults to Minecraft's main worker (for version 1.21.x).
  • block: A function containing the code to execute asynchronously.