# Math and Wait Utilities

Source: https://docs.autessa.com/docs/autessascript/helpers/utilities

> The mathUtils and waitUtils helpers: arithmetic, random numbers, and pausing script execution.

# Math and Wait Utilities

Two small helpers round out the library: `mathUtils` for arithmetic and random numbers, and `waitUtils` for pausing execution. Unlike the other helpers, these return plain values — no JSON envelopes to parse.

## mathUtils

Arithmetic that returns numbers directly. JavaScript's own `Math` and operators cover most of this too; `mathUtils` is useful when inputs arrive as strings (as [tool variables always do](/docs/autessascript/bindings.md)) and for its explicit failure modes.

  - `mathUtils.add` — number"> Adds two values. The string-accepting overload parses each argument as a number first — handy for `variables.get(...)` values without an explicit `parseFloat`. Non-numeric strings throw.
  - `mathUtils.subtract` — number"> Returns `a - b`.
  - `mathUtils.multiply` — number"> Returns `a * b`.
  - `mathUtils.divide` — number"> Returns `a / b`. Throws an error on division by zero (unlike JavaScript's `/` operator, which returns `Infinity`).
  - `mathUtils.power` — number"> Returns `base` raised to `exponent`.
  - `mathUtils.sqrt` — number"> Square root. Throws an error for negative input (unlike `Math.sqrt`, which returns `NaN`).
  - `mathUtils.random` — number"> Random number in `[0, 1)`.
  - `mathUtils.randomBetween` — number"> Random number in `[min, max)`.

```javascript
const price = mathUtils.add(variables.get("basePrice"), variables.get("surcharge")); // strings ok
const perUnit = mathUtils.divide(price, parseInt(variables.get("quantity")));        // throws if quantity is "0"
response.setResponse(JSON.stringify({ perUnit: perUnit.toFixed(2) }));
```

> `mathUtils.divide` and `mathUtils.sqrt` throw real errors instead of returning `Infinity`/`NaN`. That's a feature — a thrown error fails the execution loudly instead of letting a silent `NaN` propagate into your result — but wrap them in `try`/`catch` if you want to handle the case yourself.

## waitUtils

  - `waitUtils.sleep` — void"> Blocks the script for the given number of milliseconds. Throws an error if the wait is interrupted by the platform.

There is no cap on the argument itself, but sleeping is never free: the script is fully blocked while it sleeps, and the time counts against the surrounding execution's overall time budget. Keep sleeps short — polling backoff between `api.*` calls is the intended use:

```javascript
let status = "pending";
for (let attempt = 0; attempt < 5 && status === "pending"; attempt++) {
  if (attempt > 0) waitUtils.sleep(2000);   // 2s between polls
  const res = JSON.parse(api.get(JSON.stringify({ endpoint: jobUrl })));
  status = JSON.parse(res.body).status;
  console.log("attempt", attempt + 1, "status:", status);
}
response.setResponse(JSON.stringify({ status: status }));
```

Remember that `setTimeout` does not exist in the [sandbox](/docs/autessascript/sandbox.md) — `waitUtils.sleep` is the only way to pause, and it pauses everything.

## Related

- [The sandbox](/docs/autessascript/sandbox.md)
- [API helper](/docs/autessascript/helpers/api.md)
- [Limits and errors](/docs/autessascript/limits-errors.md)
- [AutessaScript overview](/docs/autessascript.md)

