Skip to content

timeout

ffredyk edited this page Jul 23, 2026 · 1 revision

timeout

Category

Concurrency

Arity

Binary<handle> timeout <seconds>.

<scriptHandle> timeout <number>

Description

Races a ScriptHandle against a time limit. Returns a new ScriptHandle that resolves when either the original handle completes or the timeout expires — whichever happens first.

  • If the original handle completes before timeout → new handle resolves with the original's return value.
  • If timeout expires first → new handle resolves with nil (original may still be running).

The original handle is not automatically terminated — use terminate if needed.

How It Works

Creates a composite handle that monitors both the original handle's resolution and a timer. The first to trigger resolves the composite handle. The original handle continues running even if timeout fires (unless explicitly terminated).

Usage

Basic timeout

_handle = spawn { sleep 10; return "slow result"; };
_race = _handle timeout 3;     // 3 second timeout
_result = await _race;
// _result is nil (timeout fired first)
// _handle is still running!

Timeout with termination

_handle = spawn { riskyOperation(); };
_race = _handle timeout 5;
_result = await _race;

if (isNil "_result") then {
    terminate _handle;         // stop the original
    systemChat "Operation timed out";
} else {
    systemChat f"Result: {_result}";
};

Network request pattern

_request = spawn { fetchFromServer(); };
_response = await (_request timeout 10);
if (isNil "_response") then {
    terminate _request;
    systemChat "Server not responding";
};

Multiple timeouts

_h1 = spawn { task1(); };
_h2 = spawn { task2(); };

_r1 = await (_h1 timeout 5);
_r2 = await (_h2 timeout 5);

if (isNil "_r1" || isNil "_r2") then {
    systemChat "Some tasks timed out";
};

Thread Safety

Creates a new handle on the current scheduler. The original handle continues on its scheduler.

See Also

  • await — wait for handle (no timeout)
  • sleep — simple time delay
  • terminate — force-stop handle
  • spawn — create handle

SQ# Wiki

Home

Engine Docs

Migration

Commands

Value Constructors

Arithmetic

Comparison

Logic

Array

String

Math

Random

Type & Introspection

HashMap

Code Execution

Concurrency

Scheduler

Thread Safety

Error

Output

Time

Multiplayer

Compiler

Clone this wiki locally