diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e6722..839d21c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## [Next] ### Fixed -- Make `Promise.is` work with promises from old versions of the library +- Make `Promise.is` work with promises from old versions of the library (#41) +- Make `Promise.delay` properly break out of the current loop (#40) ## [3.0.0] - 2020-08-17 ### Changed diff --git a/lib/init.lua b/lib/init.lua index bdf18f1..16ff901 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -719,7 +719,9 @@ do if connection == nil then -- first is nil when connection is nil first = node connection = Promise._timeEvent:Connect(function() - while first.endTime <= Promise._getTime() do + local threadStart = Promise._getTime() + + while first ~= nil and first.endTime < threadStart do local current = first first = current.next @@ -731,7 +733,6 @@ do end current.resolve(Promise._getTime() - current.startTime) - if current.next == nil then return end -- kill this thread if there was no `first` before `resolve` end end) else -- first is non-nil diff --git a/lib/init.spec.lua b/lib/init.spec.lua index b27b240..f27fd77 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -201,6 +201,21 @@ return function() advanceTime(1) expect(promise:getStatus()).to.equal(Promise.Status.Resolved) end) + + it("Should allow for delays to be cancelled", function() + local promise = Promise.delay(2) + + Promise.delay(1):andThen(function() + promise:cancel() + end) + + expect(promise:getStatus()).to.equal(Promise.Status.Started) + advanceTime() + expect(promise:getStatus()).to.equal(Promise.Status.Started) + advanceTime(1) + expect(promise:getStatus()).to.equal(Promise.Status.Cancelled) + advanceTime(1) + end) end) describe("Promise.resolve", function() @@ -1530,4 +1545,4 @@ return function() expect(Promise.is(oldPromise)).to.equal(true) end) end) -end \ No newline at end of file +end