Skip to content

Promise

AGF uses evaera's Promise implementation. See the documentation for that implementation.

A few modifications have been made to the Promise object for AGF. Most notably, PascalCase mappings have been added for all public methods, as well as simplified versions. For instance, the andThen method has a mapping to both AndThen as well as Then. This has been done to match the PascalCase styling of AGF.

The below documentation favors the PascalCase methods. However, the original camelCase methods still exist.


Static Methods

Promise.new(Function callback)

Create a new promise object. The callback receives a 'resolve', 'reject', and onCancel function that can then be called within the function. Call resolve when the task has completed successfully. Call reject if something went wrong.

Optionally, pass an cancellation handler to the onCancel function, which will be called if the promise gets cancelled. This can be used to stop whatever task is occurring within the promise. Read the official documentation for more information.

1
2
3
4
5
6
7
local promise = Promise.new(function(resolve, reject, onCancel)
    if thisThingWorked() then
        resolve()
    else
        reject("It didn't work")
    end
end)

Promise.Defer(Function callback)

Same as Promise.new, but will begin execution after a Heartbeat event.


Promise.Promisify(Function callback)

Creates a promise wrapper for a yielding function.


Promise.Resolve(...)

Creates an immediately-resolved promise with the given value.


Promise.Reject(...)

Creates an immediately-rejected promise with the given value.


Promise.Try(...)

Starts a promise chain and turns synchronous errors into rejections.


Promise.All(Table promises)

Creates a new promise with a list of other promises. It is resolved once all promises in the table are resolved, and is rejected if even one of the promises is rejected. This is a great method when multiple requests need to be made at the same time but do not depend on each other.


Promise.AllSettled(Table promises)

Creates a new promise with a list of other promises. It is resolved once all promises in the table are settled (i.e. after all Finally calls on each promise have been made).


Promise.Race(Table promises)

Resolves or rejects on the first promise to resolve or reject. All other promises will be cancelled.


Promise.Some(Table promises, Number amount)

Resolves once the amount number of promises have resolved. All other promises will be cancelled.


Promise.Any(Table promises)

Resolves if any of the promises resolve, and will reject if all promises are rejected. All other promises will be cancelled.


Promise.Delay(Number seconds)

Creates a promise that resolves after the number of seconds has elapsed.


Promise.Each(Table promises, Function predicate)


Promise.Retry(callback, numRetries)

The callback is a function that returns a Promise. It will continue to call the callback numRetries times until the promise resolves. If the amount of times exceeds numRetries, then the last rejected promise will be returned.


Promise.FromEvent(event [, predicate])

Wraps an event with a Promise which is resolved the next time the event is fired.


Promise.Is(Any object)

Checks to see if the passed object is a Promise.


Object Methods

Then(Function success, Function failure)


Catch(Function failure)


Tap(Function tap)


Finally(Function finally)


Done(Function done)


ThenCall(Function callback, ...)


FinallyCall(Function callback, ...)


DoneCall(Function callback, ...)


ThenReturn(...)


FinallyReturn(...)


DoneReturn(...)


Timeout(Number seconds, Variant timeoutValue)


Cancel()


Await()


AwaitStatus()


Expect(...)


GetStatus()


Properties

Status

The PromiseStatus of the promise

Types

PromiseStatus

Possible values: Started, Resolved, Rejected, Cancelled