From d47d5972596620fefffedc3351ef6b54013728e7 Mon Sep 17 00:00:00 2001 From: Eryn Lynn Date: Mon, 24 Aug 2020 13:20:47 -0400 Subject: [PATCH] Fix #41 --- CHANGELOG.md | 7 ++++++- lib/init.lua | 6 +++++- lib/init.spec.lua | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e88bc72..53e6722 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog -## [3.0.0] +## [Next] +### Fixed +- Make `Promise.is` work with promises from old versions of the library + +## [3.0.0] - 2020-08-17 +### Changed - `Promise.delay` now uses `os.clock` - Made `Promise.delay` behavior more consistent when creating new timers in the callback of a timer. diff --git a/lib/init.lua b/lib/init.lua index b5d71db..bdf18f1 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -667,7 +667,11 @@ function Promise.is(object) elseif objectMetatable == nil then -- No metatable, but we should still chain onto tables with andThen methods return type(object.andThen) == "function" - elseif type(objectMetatable) == "table" and type(rawget(objectMetatable, "andThen")) == "function" then + elseif + type(objectMetatable) == "table" + and type(rawget(objectMetatable, "__index")) == "table" + and type(rawget(rawget(objectMetatable, "__index"), "andThen")) == "function" + then -- Maybe this came from a different or older Promise library. return true end diff --git a/lib/init.spec.lua b/lib/init.spec.lua index 096e4a9..b27b240 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -1498,4 +1498,36 @@ return function() expect(promise._values[1]).to.equal("foo") end) end) + + describe("Promise.is", function() + it("should work with current version", function() + local promise = Promise.resolve(1) + + expect(Promise.is(promise)).to.equal(true) + end) + + it("should work with any object with an andThen", function() + local obj = { + andThen = function() + return 1 + end + } + + expect(Promise.is(obj)).to.equal(true) + end) + + it("should work with older promises", function() + local OldPromise = {} + OldPromise.prototype = {} + OldPromise.__index = OldPromise.prototype + + function OldPromise.prototype:andThen() + + end + + local oldPromise = setmetatable({}, OldPromise) + + expect(Promise.is(oldPromise)).to.equal(true) + end) + end) end \ No newline at end of file