diff --git a/.gitignore b/.gitignore index 5bad65a..6da598e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /luacov.*.out -node_modules/ \ No newline at end of file +node_modules/ +.vscode \ No newline at end of file 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 07781a7..16ff901 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 81ed738..f27fd77 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -1513,4 +1513,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