Skip to content

Commit

Permalink
Safety but with less freak-outs
Browse files Browse the repository at this point in the history
  • Loading branch information
VorTechnix committed Jun 6, 2024
1 parent 2c36ad6 commit 3ccea53
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions promise_tech.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Promise.then_ = function(self, onFulfilled, onRejected)
end
-- If self.state is not "pending" then error
if self.state ~= "pending" then
error("Error (Promise.then_): Promise is already " .. self.state)
return Promise.reject("Error (Promise.then_): Promise is already " .. self.state)
end

-- Make locals to collect the results of self.fn
Expand All @@ -74,9 +74,11 @@ Promise.then_ = function(self, onFulfilled, onRejected)
-- Return a new promise with the results
if success and not force_reject then
onFulfilled(result[1])
self.state = "fulfilled"
return Promise.resolve(result[1])
else
onRejected(result[1])
self.state = "rejected"
return Promise.reject(success and result[1] or err)
end
end
Expand Down Expand Up @@ -137,10 +139,10 @@ return Promise
Promise = require "promise_tech"
tmp = Promise.resolve(5)
tmp:then_(print, nil)
tmp:then_(print, nil):then_(print, nil):then_(print, nil)
tmp = Promise.reject(7)
tmp:then_(nil, print)
tmp:then_(nil, print):then_(nil, print):then_(nil, print)
--- BIG TESTS
Expand All @@ -166,4 +168,8 @@ function do_if_fails(err)
end
test():then_(do_if_passes, do_if_fails)
Vx2 = 0
test():then_(function(value) Vx2 = value end, function(value) print("caught rejection, value", value) end):
then_(function(value) print("Sqrt is", math.sqrt(value)) end)
if Vx2 ~= 0 then print("Vx2", Vx2) end
]]

0 comments on commit 3ccea53

Please sign in to comment.