This commit is contained in:
Eryn Lynn 2020-08-24 13:20:47 -04:00
parent 8ee520d839
commit d47d597259
3 changed files with 43 additions and 2 deletions

View file

@ -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.

View file

@ -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

View file

@ -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