A typed wrapper over FiveM's Lua promises, for turning callback-style APIs into blocking, straight-line code.

function fetchProfile(id:String):Profile {
    var deferred = new Deferred<Profile>();
    someResource.getProfile(id, profile -> deferred.resolve(profile));
    return deferred.await();
}

await() suspends the calling coroutine, not the resource: other threads, events and the game itself keep running. It is only valid inside a coroutine — a thread, event handler, command handler or export — which covers nearly all resource code but not the top level of main().

Resolving twice is ignored, matching promise semantics; the first settlement wins.

Static methods

staticawaitCallback<T>(register:(resolve:T ‑> Void) ‑> Void):T

Adapts a callback-style function into a blocking call.

var rows = Deferred.awaitCallback(resolve -> db.query("...", resolve));

staticresolved<T>(value:T):Deferred<T>

A promise that is already settled with value.

Constructor

new()

Variables

finalhandle:Dynamic

The underlying Lua promise, for handing to resources that expect one.

read onlyisSettled:Bool

Whether the promise has already been resolved or rejected.

Methods

await():T

Blocks the current coroutine until the promise settles, then returns the resolved value. A rejection is raised as a Lua error, catchable with an ordinary Haxe try/catch.

awaitWithTimeout(timeoutMs:Int):Null<T>

Blocks until the promise settles or timeoutMs elapses, returning null on timeout instead of hanging.

The underlying operation is not cancelled — it just stops being waited on, and a late resolution is discarded.

@:has_untypedreject(reason:Dynamic):Void

Settles the promise with a failure. await will then throw reason.

@:has_untypedresolve(value:T):Void

Settles the promise successfully, waking anything blocked in await.