> ## Documentation Index
> Fetch the complete documentation index at: https://ugcore.urging.ch/llms.txt
> Use this file to discover all available pages before exploring further.

# UgLib.Async

> Wait for a condition, delay, debounce and throttle.

<Badge color="purple">Shared</Badge> Runs in your resource.

```lua theme={"dark"}
UgLib.Async.WaitFor(check, timeout?, interval?) -> ok, value
UgLib.Async.Timeout(ms, fn) -> cancel
UgLib.Async.Debounce(fn, ms) -> function
UgLib.Async.Throttle(fn, ms) -> function
```

* `WaitFor` calls `check` every `interval` ms (default 0, every frame) until it returns a truthy value, for at most `timeout` ms (default 5000). `ok` is false on timeout. MUST run in a thread.
* `Timeout` runs `fn` after `ms` and returns a function that cancels it.
* `Debounce` returns a function that runs `fn` once calls stop for `ms`, with the arguments of the last call.
* `Throttle` returns a function that runs `fn` at most once per `ms`. The first call runs at once, calls in between are dropped.

## Example

```lua theme={"dark"}
CreateThread(function()
    local ok, vehicle = UgLib.Async.WaitFor(function()
        local entity = NetworkGetEntityFromNetworkId(netId)

        return DoesEntityExist(entity) and entity
    end, 3000)
end)
```


## Related topics

- [UgLib.Version](/ug-lib/version.md)
- [UgLib.Enums](/ug-lib/enums.md)
- [UgLib.Locale](/ug-lib/locale.md)
- [UgLib.Menu](/ug-lib/menu.md)
- [UgLib.Context](/ug-lib/context.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.