self:reject

Shared

Rejects the promise with the given reason, resuming any threads awaiting it with a failure


Syntax

local status = self:reject(
    ...
)

A promise can only be settled once. Calling self:reject or self:resolve on an already-settled promise has no effect and returns false.

  • Pending — the promise is rejected and all awaiting threads are resumed with the provided rejection values.
  • Already settled — the call is ignored and false is returned.

Parameters

TypeNameDescription
any...One or more values describing the rejection reason

Returns

TypeNameDescription
boolstatustrue if the promise was successfully rejected, false if it was already settled

Examples

Reject a promise with an error message
local entity = util.promise.create()

entity:reject("something went wrong")
Reject a promise and handle the error from a thread
local entity = util.promise.create()

entity:reject("connection timed out")

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

    if not ok then
        core.engine.print("error", err) -- 'connection timed out'
    end
end):resume()
Attempt to reject an already-settled promise
local entity = util.promise.create()

entity:resolve(true)
core.engine.print("info", entity:reject("too late")) -- false

On this page