Merge pull request #40 from Validark/patch-3

(Promise.delay) Allow OnCancel to break out of the current loop
This commit is contained in:
eryn L. K 2020-08-24 13:23:51 -04:00 committed by GitHub
commit 7fc6fc90f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 4 deletions

View file

@ -2,7 +2,8 @@
## [Next] ## [Next]
### Fixed ### 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 ## [3.0.0] - 2020-08-17
### Changed ### Changed

View file

@ -719,7 +719,9 @@ do
if connection == nil then -- first is nil when connection is nil if connection == nil then -- first is nil when connection is nil
first = node first = node
connection = Promise._timeEvent:Connect(function() 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 local current = first
first = current.next first = current.next
@ -731,7 +733,6 @@ do
end end
current.resolve(Promise._getTime() - current.startTime) 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
end) end)
else -- first is non-nil else -- first is non-nil

View file

@ -201,6 +201,21 @@ return function()
advanceTime(1) advanceTime(1)
expect(promise:getStatus()).to.equal(Promise.Status.Resolved) expect(promise:getStatus()).to.equal(Promise.Status.Resolved)
end) 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) end)
describe("Promise.resolve", function() describe("Promise.resolve", function()
@ -1530,4 +1545,4 @@ return function()
expect(Promise.is(oldPromise)).to.equal(true) expect(Promise.is(oldPromise)).to.equal(true)
end) end)
end) end)
end end