Skip to the content.

Promise

local Promise = require(Packages.Constella.Promise)

Promise.retry(fetchLeaderboard, { Attempts = 3, Delay = 0.5 })
	:Timeout(5)
	:Then(function(rows) render(rows) end)
	:Catch(function(err) warn(err) end)

Guarantees

Types

Promise<T> resolves with a T. Promise.resolve(5) is a Promise<number>, Promise.delay a Promise<number>, and Data’s store:LoadAsync(player) a Promise<Profile<T>>, so :Expect() returns a typed value.

API

Member Description
Promise.new(executor) onCancel(hook) registers cleanup. onCancel() returns whether the promise is cancelled
Promise.resolve(...) / Promise.reject(...)  
Promise.try(fn, ...) Wraps a function call, which may return a promise
Promise.delay(seconds) Resolves with the elapsed time. Cancelling it stops the timer
Promise.all(list) An array of each first value. Rejects on the first failure and cancels the rest
Promise.allSettled(list) { { Status, Value } }
Promise.race(list) Settles like the first to settle and cancels the others
Promise.retry(fn, { Attempts?, Delay?, Backoff?, ShouldRetry? }?) Retries with exponential backoff
Promise.fromEvent(signal, predicate?) The next matching event. Works with RBXScriptSignal and Constella Signal
promise:Then(onResolved?, onRejected?) / :Catch(fn) / :Finally(fn(status))  
promise:Await() (true, ...) or (false, reason)
promise:Expect() The values, or raises the rejection
promise:Timeout(seconds, value?) Rejects with a Timeout PromiseError (or value) and cancels the slow work
promise:Cancel() / promise:GetStatus() Pending, Resolved, Rejected or Cancelled
Promise.is(value) / Promise.isError(value, kind?)  
Promise.setUnhandledRejectionHandler(fn?) Replaces the reporter for every promise and returns the previous one. For tests and custom reporting

Cleanup:Add(promise) cancels the promise when the Cleanup is cleaned.

Limitations