self:await

Shared

Suspends the thread until the given promise settles, then returns its resolved values


Syntax

local status, ... = self:await(
    promise
)

This function must be called from within a running thread. It cannot be used outside a thread context, while the thread is sleeping, or while it is already awaiting another util.promise.

  • Resolved — resumes the thread with true followed by the resolved values.
  • Rejected — resumes the thread with false followed by the rejection values.
  • Already settled — returns immediately without yielding.

Parameters

TypeNameDescription
promisepromisePromise instance to await

Returns

TypeNameDescription
statusresolvedtrue if the promise resolved, false if it was rejected
any...Values the promise was settled with

Examples

Await a promise and print the resolved values
local promise = util.promise.create()

local entity = util.thread.create(function(self)
    local ok, result = self:await(promise)
    core.engine.print("info", "Resolved:", ok, result)
end)

entity:resume()
promise:resolve("done")
Handle both resolve and reject outcomes
local promise = util.promise.create()

local entity = util.thread.create(function(self)
    local ok, value = self:await(promise)

    if ok then
        core.engine.print("info", "Success:", value)
    else
        core.engine.print("error", "Failed:", value)
    end
end)

entity:resume()
promise:reject("something went wrong")

On this page