A value that arrives later: one thread settles it, and any number of others wait for it, each with its own timeout.
Make one with Promise.new() and settle it once with SetValue or Fail, or let Promise.Spawn(work, ...) run work in a new thread and settle with its first result. false and nil are real values, so Settled says whether it has one. Promise.AllAsync waits for a whole list, and settling twice is an error.
--!strict-- An order that arrives later: one thread settles it, the program waits for it.local Promise = require("@shotsystem/Promise")
local TimeService = require("@shotsystem/TimeService")
local order = Promise.new()
order.Done:Connect(function(value : any, why : string?)
print("Done fired with:", value, why)
end)
Promise.Spawn(function()
TimeService:WaitAsync(0.1)
order:SetValue("Apple")
end)
print("before:", order.Settled, tostring(order))
print("arrived:", order:GetValueAsync(5))
print("after:", order.Settled, order.Value, tostring(order))
-- false is a real value, a wait can time out, and a failure comes back as whylocal empty = Promise.new()
empty:SetValue(false)
print("false:", empty:GetValueAsync())
print("never settled:", Promise.new():GetValueAsync(0.1))
local sold = Promise.new()
sold:Fail("out of stock")
print("failed:", sold:GetValueAsync())
-- Spawn runs a function in a thread of its own; AllAsync waits for them alllocal price = Promise.Spawn(function(count : number) return count * 3end, 4)
local shop = Promise.Spawn(function() return"Shop"end)
local values = Promise.AllAsync({ price, shop })
print("all:", values and values[1], values and values[2], Promise.Is(price), Promise.Is({}))
What it printedOutput
before: false Promise(pending)
Done fired with: Apple nil
arrived: Apple nil
after: true Apple Promise(Apple)
false: false nil
never settled: nil timed out
failed: nil out of stock
all: 12 Shop true false