Object MethodCore Flow

Workflow.waitUntil(predicate, options?)

Polls a predicate until it returns a truthy value, times out or reaches the attempt limit, returning a structured result.

Documented for app version 1.0.50Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

Workflow is a global table: call its functions with a dot (Workflow.<method>(...)), not a colon. Requires Macro Handler 1.0.51 or later.

When To Use It

Polls a predicate until it returns a truthy value, times out or reaches the attempt limit, returning a structured result.

Parameters and Return

predicate(attempt); options: timeoutMs, intervalMs (default 100), maxAttempts (default 10000), accept(value, attempt, values). An ordinary predicate error returns reason="error". The result has ok, reason, attempts, elapsedMs, value, values and error; values.n preserves the number of multiple returns, including nil. The default timeout is 30000 ms, with a maximum of 86400000 ms and up to 1000000 iterations. Calls are synchronous and cooperative; a blocking operation must still use its own timeout.

Best Combined With

Combine API calls, conditions and reusable Module functions. Stop, pause and execution budgets remain active. Retry actions must be safe to repeat; do not automatically repeat side effects such as payments or taps after an ambiguous network response.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

local checks = 0
local result = Workflow.waitUntil(function(attempt)
  checks = checks + 1
  return checks >= 3
end, {timeoutMs = 1000, intervalMs = 50})
print(result.ok, result.reason, result.attempts)

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
local checks = 0
local result = Workflow.waitUntil(function(attempt)
  checks = checks + 1
  return checks >= 3
end, {timeoutMs = 1000, intervalMs = 50})
print(result.ok, result.reason, result.attempts)

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
local checks = 0
local result = Workflow.waitUntil(function(attempt)
  checks = checks + 1
  return checks >= 3
end, {timeoutMs = 1000, intervalMs = 50})
print(result.ok, result.reason, result.attempts)
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  local checks = 0
  local result = Workflow.waitUntil(function(attempt)
    checks = checks + 1
    return checks >= 3
  end, {timeoutMs = 1000, intervalMs = 50})
  print(result.ok, result.reason, result.attempts)
end)

if not ok then
  print("API step failed: Workflow.waitUntil: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- Workflow.waitUntil(predicate, options?)
local function run_waituntil_step()
  local checks = 0
  local result = Workflow.waitUntil(function(attempt)
    checks = checks + 1
    return checks >= 3
  end, {timeoutMs = 1000, intervalMs = 50})
  print(result.ok, result.reason, result.attempts)
end

local ok, err = pcall(run_waituntil_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
local checks = 0
local result = Workflow.waitUntil(function(attempt)
  checks = checks + 1
  return checks >= 3
end, {timeoutMs = 1000, intervalMs = 50})
print(result.ok, result.reason, result.attempts)