From 292e47293c156847d9b685820634da981f3ff74e Mon Sep 17 00:00:00 2001 From: Eryn Lynn Date: Wed, 6 May 2020 14:20:34 -0400 Subject: [PATCH] Let Promise:expect() throw rejection objects --- CHANGELOG.md | 1 + lib/init.lua | 10 +++++----- lib/init.spec.lua | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 914ff14..ad9eed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Promise.async has been renamed to Promise.defer (Promise.async references same function for compatibility) - Improve test coverage for asynchronous and time-driven functions - Change Promise.is to be safe when dealing with tables that have an `__index` metamethod that creates an error. +- Let Promise:expect() throw rejection objects # 2.5.1 diff --git a/lib/init.lua b/lib/init.lua index 74684f6..e7f7c03 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -932,13 +932,13 @@ end --[[ Calls awaitStatus internally, returns (isResolved, values...) ]] -function Promise.prototype:await(...) - return awaitHelper(self:awaitStatus(...)) +function Promise.prototype:await() + return awaitHelper(self:awaitStatus()) end local function expectHelper(status, ...) if status ~= Promise.Status.Resolved then - error((...) == nil and "" or tostring((...)), 3) + error((...) == nil and "Expected Promise rejected with no value." or (...), 3) end return ... @@ -948,8 +948,8 @@ end Calls await and only returns if the Promise resolves. Throws if the Promise rejects or gets cancelled. ]] -function Promise.prototype:expect(...) - return expectHelper(self:awaitStatus(...)) +function Promise.prototype:expect() + return expectHelper(self:awaitStatus()) end -- Backwards compatibility diff --git a/lib/init.spec.lua b/lib/init.spec.lua index f30d4fc..f90afb6 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -1161,4 +1161,32 @@ return function() expect(promises[3]:getStatus()).to.equal(Promise.Status.Started) end) end) + + describe("Promise:await", function() + it("should return the correct values", function() + local promise = Promise.resolve(5, 6, nil, 7) + + local a, b, c, d, e = promise:await() + + expect(a).to.equal(true) + expect(b).to.equal(5) + expect(c).to.equal(6) + expect(d).to.equal(nil) + expect(e).to.equal(7) + end) + end) + + describe("Promise:expect", function() + it("should throw the correct values", function() + local rejectionValue = {} + local promise = Promise.reject(rejectionValue) + + local success, value = pcall(function() + promise:expect() + end) + + expect(success).to.equal(false) + expect(value).to.equal(rejectionValue) + end) + end) end \ No newline at end of file