Skip to content

Commit 7fc6fc9

Browse files
authored
Merge pull request #40 from Validark/patch-3
(Promise.delay) Allow OnCancel to break out of the current loop
2 parents d47d597 + d20535d commit 7fc6fc9

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## [Next]
44
### Fixed
5-
- Make `Promise.is` work with promises from old versions of the library
5+
- Make `Promise.is` work with promises from old versions of the library (#41)
6+
- Make `Promise.delay` properly break out of the current loop (#40)
67

78
## [3.0.0] - 2020-08-17
89
### Changed

lib/init.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,9 @@ do
719719
if connection == nil then -- first is nil when connection is nil
720720
first = node
721721
connection = Promise._timeEvent:Connect(function()
722-
while first.endTime <= Promise._getTime() do
722+
local threadStart = Promise._getTime()
723+
724+
while first ~= nil and first.endTime < threadStart do
723725
local current = first
724726
first = current.next
725727

@@ -731,7 +733,6 @@ do
731733
end
732734

733735
current.resolve(Promise._getTime() - current.startTime)
734-
if current.next == nil then return end -- kill this thread if there was no `first` before `resolve`
735736
end
736737
end)
737738
else -- first is non-nil

lib/init.spec.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ return function()
201201
advanceTime(1)
202202
expect(promise:getStatus()).to.equal(Promise.Status.Resolved)
203203
end)
204+
205+
it("Should allow for delays to be cancelled", function()
206+
local promise = Promise.delay(2)
207+
208+
Promise.delay(1):andThen(function()
209+
promise:cancel()
210+
end)
211+
212+
expect(promise:getStatus()).to.equal(Promise.Status.Started)
213+
advanceTime()
214+
expect(promise:getStatus()).to.equal(Promise.Status.Started)
215+
advanceTime(1)
216+
expect(promise:getStatus()).to.equal(Promise.Status.Cancelled)
217+
advanceTime(1)
218+
end)
204219
end)
205220

206221
describe("Promise.resolve", function()
@@ -1530,4 +1545,4 @@ return function()
15301545
expect(Promise.is(oldPromise)).to.equal(true)
15311546
end)
15321547
end)
1533-
end
1548+
end

0 commit comments

Comments
 (0)