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"
end
function Promise:getStatus()
return self._status
end
--[[
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(resolveArg).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)
it("should resolve promises on resolve()", function()
@ -37,7 +37,7 @@ return function()
expect(promise).to.be.ok()
expect(callCount).to.equal(1)
expect(promise._status).to.equal(Promise.Status.Resolved)
expect(promise:getStatus()).to.equal(Promise.Status.Resolved)
end)
it("should reject promises on reject()", function()
@ -50,7 +50,7 @@ return function()
expect(promise).to.be.ok()
expect(callCount).to.equal(1)
expect(promise._status).to.equal(Promise.Status.Rejected)
expect(promise:getStatus()).to.equal(Promise.Status.Rejected)
end)
it("should reject on error in callback", function()
@ -63,7 +63,7 @@ return function()
expect(promise).to.be.ok()
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()
-- Loosely check for the pieces of the stack trace we expect
@ -78,7 +78,7 @@ return function()
local promise = Promise.resolve(5)
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)
end)
@ -88,7 +88,7 @@ return function()
end))
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)
end)
end)
@ -98,7 +98,7 @@ return function()
local promise = Promise.reject(6)
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)
end)
@ -110,7 +110,7 @@ return function()
local promise = Promise.reject(innerPromise)
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)
end)
end)
@ -140,12 +140,12 @@ return function()
expect(args[1]).to.equal(5)
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(chained).to.be.ok()
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)
end)
@ -173,12 +173,12 @@ return function()
expect(args[1]).to.equal(5)
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(chained).to.be.ok()
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)
end)
@ -214,12 +214,12 @@ return function()
expect(args[1]).to.equal(6)
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(chained).to.be.ok()
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)
end)
@ -255,12 +255,12 @@ return function()
expect(args[1]).to.equal(6)
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(chained).to.be.ok()
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)
end)
end)