Skip to content

Async Handling in Form Component Functions

VennV edited this page Jul 28, 2024 · 4 revisions
  • For example, you want to handle the asynchrony of a Form component in the form of CUSTOM_FORM.
...
use vennv\vapm\Async;
use vennv\vapm\Promise;

     /**
     * @throws Throwable
     *
     * This is an example of how to use Async
     */
    #[VToggle(
        text: "Test StepSlider Async",
        default: true
    )]
    public function toggleAsync(Player $player, mixed $data): Async
    {
        return new Async(function () use ($player, $data) {
            $player->sendMessage("Toggle Async: You have selected: " . ($data ? "true" : "false"));
        });
    }

    /**
     * @throws Throwable
     *
     * This is an example of how to use Promise
     */
    #[VToggle(
        text: "Test StepSlider Promise",
        default: true
    )]
    public function togglePromise(Player $player, mixed $data): Promise
    {
        return new Promise(function ($resolve) use ($player, $data) {
            $player->sendMessage("Toggle Promise: You have selected: " . ($data ? "true" : "false"));
            $resolve();
        });
    }

Can you read more here: Click

Clone this wiki locally