Skip to content

Commit

Permalink
proper error propagation across promises
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Sep 14, 2024
1 parent a78d9f7 commit d93de17
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 8 additions & 6 deletions promise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ local State = {
REJECTED = 'rejected',
}

local passthrough = function(x) return x end
local errorthrough = function(x) error(x) end

local function callable_table(callback)
local mt = getmetatable(callback)
return type(mt) == 'table' and type(mt.__call) == 'function'
Expand Down Expand Up @@ -141,12 +138,17 @@ run = function(promise)
i = i + 1
local obj = q[i]
local success, result = pcall(function()
local success = obj.fulfill or passthrough
local failure = obj.reject or errorthrough
local callback = promise.state == State.FULFILLED and success or failure
local success_fn = obj.fulfill or function(x) return x end
local failure_fn = obj.reject or function(x) error(x) end
local callback = promise.state == State.FULFILLED and success_fn or failure_fn
return callback(promise.value)
end)

if not success and obj.reject == nil then
-- replace reason
result = promise.value
end

if not success then
reject(obj.promise, result)
else
Expand Down
3 changes: 1 addition & 2 deletions promise.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ mtt.register("error handling 2", function(callback)
end)

Promise.rejected("nope"):next(function() end):catch(function(err)
-- "/home/user/.minetest/mods/promise/promise.lua:13: nope"
assert(err)
assert(err == "nope")
callback()
end)
end)
Expand Down

0 comments on commit d93de17

Please sign in to comment.