util.promise.create

Shared

Creates a new promise instance in pending state


Syntax

local instance = util.promise.create()

A promise starts in a pending state and must be explicitly settled. Use self:resolve or self:reject to settle the promise, and thread:await inside a thread to retrieve the result.

  • Resolved — the operation completed successfully; awaiting returns the resolved values.
  • Rejected — the operation failed; awaiting throws with the rejection reason.

Parameters

No parameters are accepted by this function

Returns

TypeNameDescription
promiseinstanceCreated promise instance in a pending state

Examples

Create and resolve a promise
local entity = util.promise.create()

entity:resolve(true, "done")
Create a promise and await it inside a thread
local entity = util.promise.create()

util.thread.create(function(self)
    local value = self:await(entity)
    core.engine.print("info", value) -- 42
end):resume()

util.timer.create(function()
    entity:resolve(42)
end, 1000, 1)

On this page