mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-25 16:10:02 +00:00
Update await to use coroutine.yield
And add compatability with cancellation by returning nil.
This commit is contained in:
parent
617c2753bc
commit
cda921e793
1 changed files with 26 additions and 15 deletions
27
lib/init.lua
27
lib/init.lua
|
@ -389,23 +389,31 @@ function Promise.prototype:await()
|
||||||
self._unhandledRejection = false
|
self._unhandledRejection = false
|
||||||
|
|
||||||
if self._status == Promise.Status.Started then
|
if self._status == Promise.Status.Started then
|
||||||
local result
|
local ok, result, resultLength
|
||||||
local resultLength
|
|
||||||
local bindable = Instance.new("BindableEvent")
|
|
||||||
|
|
||||||
|
local thread = coroutine.running()
|
||||||
|
|
||||||
|
spawn(function()
|
||||||
self:andThen(
|
self:andThen(
|
||||||
function(...)
|
function(...)
|
||||||
resultLength, result = pack(...)
|
resultLength, result = pack(...)
|
||||||
bindable:Fire(true)
|
ok = true
|
||||||
end,
|
end,
|
||||||
function(...)
|
function(...)
|
||||||
resultLength, result = pack(...)
|
resultLength, result = pack(...)
|
||||||
bindable:Fire(false)
|
ok = false
|
||||||
end
|
end
|
||||||
)
|
):finally(function()
|
||||||
|
coroutine.resume(thread)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
local ok = bindable.Event:Wait()
|
coroutine.yield()
|
||||||
bindable:Destroy()
|
|
||||||
|
if ok == nil then
|
||||||
|
-- If cancelled, we return nil.
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
return ok, unpack(result, 1, resultLength)
|
return ok, unpack(result, 1, resultLength)
|
||||||
elseif self._status == Promise.Status.Resolved then
|
elseif self._status == Promise.Status.Resolved then
|
||||||
|
@ -413,6 +421,9 @@ function Promise.prototype:await()
|
||||||
elseif self._status == Promise.Status.Rejected then
|
elseif self._status == Promise.Status.Rejected then
|
||||||
return false, unpack(self._values, 1, self._valuesLength)
|
return false, unpack(self._values, 1, self._valuesLength)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- If the promise is cancelled, fall through to nil.
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
|
|
Loading…
Reference in a new issue