Safety but with less freak-outs

This commit is contained in:
VorTechnix 2024-06-06 13:12:21 -07:00
parent 2c36ad600a
commit 3ccea536c8
No known key found for this signature in database
GPG key ID: 091E91A69545D5BA

View file

@ -55,7 +55,7 @@ Promise.then_ = function(self, onFulfilled, onRejected)
end end
-- If self.state is not "pending" then error -- If self.state is not "pending" then error
if self.state ~= "pending" then 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 end
-- Make locals to collect the results of self.fn -- Make locals to collect the results of self.fn
@ -74,9 +74,11 @@ Promise.then_ = function(self, onFulfilled, onRejected)
-- Return a new promise with the results -- Return a new promise with the results
if success and not force_reject then if success and not force_reject then
onFulfilled(result[1]) onFulfilled(result[1])
self.state = "fulfilled"
return Promise.resolve(result[1]) return Promise.resolve(result[1])
else else
onRejected(result[1]) onRejected(result[1])
self.state = "rejected"
return Promise.reject(success and result[1] or err) return Promise.reject(success and result[1] or err)
end end
end end
@ -137,10 +139,10 @@ return Promise
Promise = require "promise_tech" Promise = require "promise_tech"
tmp = Promise.resolve(5) tmp = Promise.resolve(5)
tmp:then_(print, nil) tmp:then_(print, nil):then_(print, nil):then_(print, nil)
tmp = Promise.reject(7) tmp = Promise.reject(7)
tmp:then_(nil, print) tmp:then_(nil, print):then_(nil, print):then_(nil, print)
--- BIG TESTS --- BIG TESTS
@ -166,4 +168,8 @@ function do_if_fails(err)
end end
test():then_(do_if_passes, do_if_fails) 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
]] ]]