# Async / waiting

## Summary

Published: Feb 18 2024 by [mana vortex](mailto:undefined)\
Last documented edit: Feb 18 2024 by [mana vortex](mailto:undefined)

## Where to get cron

You can get [Cron.lua](https://github.com/psiberx/cp2077-cet-kit/blob/main/Cron.lua) from psiberx's [cp2077-cet-kit](https://github.com/psiberx/cp2077-cet-kit) on github. Add it to your sources, and then define it as a global:

```lua
-- don't overwrite it if it's already defined in the global scope. Or do, up to you.
Cron = Cron or require('External/Cron.lua')
```

## How to use cron

Cron offers a bunch of functions for executing code later. The code you want to execute has to be inside a function (=> callback) and is passed among the arguments.&#x20;

For periodically running functions, you need to include logic when to stop the execution.

### Performance impact

To save processor load, it is  best practice to define your callback outside of the function call. This can have a noticable performance impact, so you're gonna want to do this.

#### Good example:

<pre class="language-lua"><code class="lang-lua">local callback = function()
    -- do a little dance, make a little love
<strong>end
</strong>Cron.Every(0.1, callback)
</code></pre>

The callback is defined once.

#### Bad example:

<pre class="language-lua"><code class="lang-lua">Cron.Every(0.1, function()
    -- do a little dance, make a little love
<strong>end)
</strong></code></pre>

The callback is defined every time cron runs.

### Passing function arguments

To pass arguments to your callback function, you pass them as arguments to cron:

<pre class="language-lua"><code class="lang-lua">local callback = function(timer)
    timer.tick = timer.tick +1
    -- do a little dance, make a little love
<strong>end
</strong>Cron.Every(0.1, { tick = 1 }, callback)
</code></pre>

## Cron functions

### Delay

Execute the callback after X seconds

```lua
local callback = function() --[[ do a little dance, make a little love ]] end
Cron.After(0.2, callback)
```

### Timer

Executes a function every X seconds.

{% hint style="danger" %}
&#x20;The function will run forever unless you stop it!
{% endhint %}

```lua
local callback = function(timer)
    timer.tick = timer.tick +1
    spdlog.info("Timer is running: " .. tostring(timer.tick))
    -- do a little dance, make a little love
    if timer.tick > 150 then
        Cron.Halt(timer)
    end
end

-- define outside of function scope to re-use this in Cron.Pause example below
local timerArg = { tick = 1 }

-- start timer
Cron.Every(0.2, timerArg, callback)
```

#### Pausing and resuming a timer

You can pause a timer by passing its reference to cron:

<pre class="language-lua"><code class="lang-lua"><strong>-- callback and timer have been defined in the example above
</strong><strong>-- the timer is running
</strong>
Cron.Pause(timerArg)
spdlog.info("Timer is paused after " .. tostring(timerArg.tick) .. " ticks")

-- wait 20 seconds, then resume our function
Cron.After(20, function () Cron.Resume(timerArg) end)
</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.redmodding.org/cyber-engine-tweaks/snippets-examples/utilities/async-waiting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
