Add Promise:getStatus() for checking on a promise's current status

This commit is contained in:
Lucien Greathouse 2018-06-16 19:40:57 -07:00
parent 8828d467c4
commit 5010fa389a
2 changed files with 20 additions and 16 deletions

View file

@ -157,6 +157,10 @@ function Promise.is(object)
return object._type == "Promise" return object._type == "Promise"
end end
function Promise:getStatus()
return self._status
end
--[[ --[[
Creates a new promise that receives the result of this promise. Creates a new promise that receives the result of this promise.

View file

@ -24,7 +24,7 @@ return function()
expect(callCount).to.equal(1) expect(callCount).to.equal(1)
expect(resolveArg).to.be.a("function") expect(resolveArg).to.be.a("function")
expect(rejectArg).to.be.a("function") expect(rejectArg).to.be.a("function")
expect(promise._status).to.equal(Promise.Status.Started) expect(promise:getStatus()).to.equal(Promise.Status.Started)
end) end)
it("should resolve promises on resolve()", function() it("should resolve promises on resolve()", function()
@ -37,7 +37,7 @@ return function()
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(callCount).to.equal(1) expect(callCount).to.equal(1)
expect(promise._status).to.equal(Promise.Status.Resolved) expect(promise:getStatus()).to.equal(Promise.Status.Resolved)
end) end)
it("should reject promises on reject()", function() it("should reject promises on reject()", function()
@ -50,7 +50,7 @@ return function()
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(callCount).to.equal(1) expect(callCount).to.equal(1)
expect(promise._status).to.equal(Promise.Status.Rejected) expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
end) end)
it("should reject on error in callback", function() it("should reject on error in callback", function()
@ -63,7 +63,7 @@ return function()
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(callCount).to.equal(1) expect(callCount).to.equal(1)
expect(promise._status).to.equal(Promise.Status.Rejected) expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
expect(promise._value[1]:find("hahah")).to.be.ok() expect(promise._value[1]:find("hahah")).to.be.ok()
-- Loosely check for the pieces of the stack trace we expect -- Loosely check for the pieces of the stack trace we expect
@ -78,7 +78,7 @@ return function()
local promise = Promise.resolve(5) local promise = Promise.resolve(5)
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Resolved) expect(promise:getStatus()).to.equal(Promise.Status.Resolved)
expect(promise._value[1]).to.equal(5) expect(promise._value[1]).to.equal(5)
end) end)
@ -88,7 +88,7 @@ return function()
end)) end))
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Rejected) expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
expect(promise._value[1]).to.equal(7) expect(promise._value[1]).to.equal(7)
end) end)
end) end)
@ -98,7 +98,7 @@ return function()
local promise = Promise.reject(6) local promise = Promise.reject(6)
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Rejected) expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
expect(promise._value[1]).to.equal(6) expect(promise._value[1]).to.equal(6)
end) end)
@ -110,7 +110,7 @@ return function()
local promise = Promise.reject(innerPromise) local promise = Promise.reject(innerPromise)
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Rejected) expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
expect(promise._value[1]).to.equal(innerPromise) expect(promise._value[1]).to.equal(innerPromise)
end) end)
end) end)
@ -140,12 +140,12 @@ return function()
expect(args[1]).to.equal(5) expect(args[1]).to.equal(5)
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Resolved) expect(promise:getStatus()).to.equal(Promise.Status.Resolved)
expect(promise._value[1]).to.equal(5) expect(promise._value[1]).to.equal(5)
expect(chained).to.be.ok() expect(chained).to.be.ok()
expect(chained).never.to.equal(promise) expect(chained).never.to.equal(promise)
expect(chained._status).to.equal(Promise.Status.Resolved) expect(chained:getStatus()).to.equal(Promise.Status.Resolved)
expect(#chained._value).to.equal(0) expect(#chained._value).to.equal(0)
end) end)
@ -173,12 +173,12 @@ return function()
expect(args[1]).to.equal(5) expect(args[1]).to.equal(5)
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Rejected) expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
expect(promise._value[1]).to.equal(5) expect(promise._value[1]).to.equal(5)
expect(chained).to.be.ok() expect(chained).to.be.ok()
expect(chained).never.to.equal(promise) expect(chained).never.to.equal(promise)
expect(chained._status).to.equal(Promise.Status.Resolved) expect(chained:getStatus()).to.equal(Promise.Status.Resolved)
expect(#chained._value).to.equal(0) expect(#chained._value).to.equal(0)
end) end)
@ -214,12 +214,12 @@ return function()
expect(args[1]).to.equal(6) expect(args[1]).to.equal(6)
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Resolved) expect(promise:getStatus()).to.equal(Promise.Status.Resolved)
expect(promise._value[1]).to.equal(6) expect(promise._value[1]).to.equal(6)
expect(chained).to.be.ok() expect(chained).to.be.ok()
expect(chained).never.to.equal(promise) expect(chained).never.to.equal(promise)
expect(chained._status).to.equal(Promise.Status.Resolved) expect(chained:getStatus()).to.equal(Promise.Status.Resolved)
expect(#chained._value).to.equal(0) expect(#chained._value).to.equal(0)
end) end)
@ -255,12 +255,12 @@ return function()
expect(args[1]).to.equal(6) expect(args[1]).to.equal(6)
expect(promise).to.be.ok() expect(promise).to.be.ok()
expect(promise._status).to.equal(Promise.Status.Rejected) expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
expect(promise._value[1]).to.equal(6) expect(promise._value[1]).to.equal(6)
expect(chained).to.be.ok() expect(chained).to.be.ok()
expect(chained).never.to.equal(promise) expect(chained).never.to.equal(promise)
expect(chained._status).to.equal(Promise.Status.Resolved) expect(chained:getStatus()).to.equal(Promise.Status.Resolved)
expect(#chained._value).to.equal(0) expect(#chained._value).to.equal(0)
end) end)
end) end)