diff --git a/lib/init.lua b/lib/init.lua index e48780a..34efa19 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -389,23 +389,31 @@ function Promise.prototype:await() self._unhandledRejection = false if self._status == Promise.Status.Started then - local result - local resultLength - local bindable = Instance.new("BindableEvent") + local ok, result, resultLength - self:andThen( - function(...) - resultLength, result = pack(...) - bindable:Fire(true) - end, - function(...) - resultLength, result = pack(...) - bindable:Fire(false) - end - ) + local thread = coroutine.running() - local ok = bindable.Event:Wait() - bindable:Destroy() + spawn(function() + self:andThen( + function(...) + resultLength, result = pack(...) + ok = true + end, + function(...) + resultLength, result = pack(...) + ok = false + end + ):finally(function() + coroutine.resume(thread) + end) + end) + + coroutine.yield() + + if ok == nil then + -- If cancelled, we return nil. + return nil + end return ok, unpack(result, 1, resultLength) elseif self._status == Promise.Status.Resolved then @@ -413,6 +421,9 @@ function Promise.prototype:await() elseif self._status == Promise.Status.Rejected then return false, unpack(self._values, 1, self._valuesLength) end + + -- If the promise is cancelled, fall through to nil. + return nil end --[[