Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/promise/src/Shared/Promise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ export type Promise<T...> = typeof(setmetatable(
@return boolean
]=]
function Promise.isPromise(value: any): boolean
return type(value) == "table" and value.ClassName == "Promise"
if type(value) ~= "table" then
return false
end

-- Read the metatable directly: indexing the value runs its __index, and some objects error on
-- unknown keys. Promises from another copy of this package have their own metatable.
local metatable = getmetatable(value)
return type(metatable) == "table" and rawget(metatable, "ClassName") == "Promise"
end

--[=[
Expand Down
17 changes: 17 additions & 0 deletions src/promise/src/Shared/Promise.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ describe("Promise.isPromise", function()
expect(Promise.isPromise({})).toEqual(false)
expect(Promise.isPromise({ ClassName = "NotAPromise" })).toEqual(false)
end)

it("returns false without indexing objects that error on unknown keys", function()
local strict = setmetatable({}, {
__index = function(_, key)
error(string.format("%s is not a valid member", tostring(key)))
end,
})

expect(Promise.isPromise(strict)).toEqual(false)
end)

it("returns true for a promise from another copy of the package", function()
local OtherPromise = { ClassName = "Promise" }
OtherPromise.__index = OtherPromise

expect(Promise.isPromise(setmetatable({}, OtherPromise))).toEqual(true)
end)
end)

describe("Promise.new", function()
Expand Down
Loading