mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 23:50:03 +00:00
Make multiple return values be handled correctly in all (?) cases (#3)
This commit is contained in:
parent
e785799141
commit
e6b43a1fae
1 changed files with 33 additions and 14 deletions
45
lib/init.lua
45
lib/init.lua
|
@ -4,15 +4,32 @@
|
||||||
|
|
||||||
local PROMISE_DEBUG = false
|
local PROMISE_DEBUG = false
|
||||||
|
|
||||||
local function wpcall(f, ...)
|
--[[
|
||||||
local args = {...}
|
Packs a number of arguments into a table and returns its length.
|
||||||
local argsLength = select("#", ...)
|
|
||||||
|
|
||||||
local result = {
|
Used to cajole varargs without dropping sparse values.
|
||||||
xpcall(function()
|
]]
|
||||||
|
local function pack(...)
|
||||||
|
local len = select("#", ...)
|
||||||
|
|
||||||
|
return len, { ... }
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
wpcallPacked is a version of xpcall that:
|
||||||
|
* Returns the length of the result first
|
||||||
|
* Returns the result packed into a table
|
||||||
|
* Passes extra arguments through to the passed function, which xpcall does not
|
||||||
|
* Issues a warning if PROMISE_DEBUG is enabled
|
||||||
|
]]
|
||||||
|
local function wpcallPacked(f, ...)
|
||||||
|
local argsLength, args = pack(...)
|
||||||
|
|
||||||
|
local body = function()
|
||||||
return f(unpack(args, 1, argsLength))
|
return f(unpack(args, 1, argsLength))
|
||||||
end, debug.traceback)
|
end
|
||||||
}
|
|
||||||
|
local resultLength, result = pack(xpcall(body, debug.traceback))
|
||||||
|
|
||||||
-- If promise debugging is on, warn whenever a pcall fails.
|
-- If promise debugging is on, warn whenever a pcall fails.
|
||||||
-- This is useful for debugging issues within the Promise implementation
|
-- This is useful for debugging issues within the Promise implementation
|
||||||
|
@ -21,7 +38,7 @@ local function wpcall(f, ...)
|
||||||
warn(result[2])
|
warn(result[2])
|
||||||
end
|
end
|
||||||
|
|
||||||
return unpack(result)
|
return resultLength, result
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
|
@ -30,13 +47,13 @@ end
|
||||||
]]
|
]]
|
||||||
local function createAdvancer(callback, resolve, reject)
|
local function createAdvancer(callback, resolve, reject)
|
||||||
return function(...)
|
return function(...)
|
||||||
local result = { wpcall(callback, ...) }
|
local resultLength, result = wpcallPacked(callback, ...)
|
||||||
local ok = table.remove(result, 1)
|
local ok = result[1]
|
||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
resolve(unpack(result))
|
resolve(unpack(result, 2, resultLength))
|
||||||
else
|
else
|
||||||
reject(unpack(result))
|
reject(unpack(result, 2, resultLength))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -114,7 +131,9 @@ function Promise.new(callback)
|
||||||
promise:_reject(...)
|
promise:_reject(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, err = wpcall(callback, resolve, reject)
|
local _, result = wpcallPacked(callback, resolve, reject)
|
||||||
|
local ok = result[1]
|
||||||
|
local err = result[2]
|
||||||
|
|
||||||
if not ok and promise._status == Promise.Status.Started then
|
if not ok and promise._status == Promise.Status.Started then
|
||||||
reject(err)
|
reject(err)
|
||||||
|
|
Loading…
Reference in a new issue