Promise Since 1.0.0
Promises are a way to handle asynchronous code in a synchronous manner. They are similar to the Promise
class in JavaScript. This documentation assumes you already have a basic understanding of promises.
Functions
new Since 1.0.0
Creates a new promise.
(
Callback: (Resolve: (...any) -> (), Reject: (...any) -> ()) -> (),
) -> Promise
This function immediately calls the callback in a new thread with two functions, Resolve and Reject. These functions are used, as their names imply, to resolve or reject the promise.
TIP
You can pass multiple values to Resolve and Reject.
WARNING
Resolve and reject can only be called once. On the next execution step after either of these functions are called, the promise thread will be closed.
Promise.new(function(Resolve, Reject)
Resolve("Hello, world!")
end)
Resolve Since 1.0.0
Creates a new promise that is already resolved.
(...any) -> Promise
This function creates a new promise that is already resolved with the values passed to it.
TIP
Use this function for synchronous code, it's about 10x faster than Promise.new
.
Promise.Resolve("Hello, world!")
Reject Since 1.0.0
Creates a new promise that is already rejected.
(...any) -> Promise
This function creates a new promise that is already rejected with the values passed to it.
TIP
Use this function for synchronous code, it's about 10x faster than Promise.new
.
Promise.Reject("Hello, world!")
Methods
Then Since 1.0.0
Chains from a promise to another promise.
(
OnResolve: ((...any) -> (...any))?,
OnReject: ((...any) -> (...any))?,
) -> Promise
This function returns a new promise that is resolved or rejected based on the return value of the callback functions. This should exactly match the A+ Spec.
Promise.new(function(Resolve, Reject)
Resolve("Hello, world!")
end):Then(function(Value)
print(Value) -- Hello, world!
end)
Catch Since 1.0.0
Chains from a promise to another promise, but only if the promise is rejected.
(
OnReject: ((...any) -> (...any))?,
) -> Promise
This function is the equivalent of Promise:Then(nil, OnReject)
.
Promise.new(function(Resolve, Reject)
Reject("Hello, world!")
end):Catch(function(Value)
print(Value) -- Hello, world!
end)
Finally Since 1.0.0
Chains from a promise to another promise, but is always called.
(
OnFinally: ((...any) -> (...any))?,
) -> Promise
This function is the equivalent of Promise:Then(OnFinally, OnFinally)
.
Promise.new(function(Resolve, Reject)
Resolve("Hello, world!")
end):Finally(function(Value)
print(Value) -- Hello, world!
end)
Await Yields Since 1.0.0
Waits for a promise to resolve or reject.
() -> (...any)
This function will wait for the promise to resolve or reject. If the promise is resolved, the values passed to Resolve will be returned. If the promise is rejected, the call will error with the first rejection value.
Promise.new(function(Resolve, Reject)
Resolve("Hello, world!")
end):Await() -- Hello, world!