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]
### 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

View file

@ -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

View file

@ -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
end