From 4586cc5a90d5752b0fbe6e5221463ed6ec56858d Mon Sep 17 00:00:00 2001 From: David Duque Date: Sat, 2 May 2020 19:24:05 +0100 Subject: [PATCH 1/2] Use pcall() on Promise.is when looking for the .andThen function Some tables might have strict metatables that error if a non-existant member is indexed, causing the chain to fail. --- lib/init.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/init.lua b/lib/init.lua index 1ab7e14..6687e66 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -471,7 +471,11 @@ function Promise.is(object) return false end - return type(object.andThen) == "function" + local ok, isPromise = pcall(function() + return type(object.andThen) == "function" + end) + + return ok and isPromise end --[[ From 9cf49499bd7844ce8e81e3cacdab05b7fea42df1 Mon Sep 17 00:00:00 2001 From: David Duque Date: Mon, 4 May 2020 20:19:54 +0100 Subject: [PATCH 2/2] Apply suggestions Co-authored-by: eryn L. K. --- lib/init.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index 6687e66..9d3b0fa 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -471,11 +471,20 @@ function Promise.is(object) return false end - local ok, isPromise = pcall(function() - return type(object.andThen) == "function" - end) + local objectMetatable = getmetatable(object) - return ok and isPromise + if objectMetatable == Promise then + -- The Promise came from this library. + return true + 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 + -- Maybe this came from a different or older Promise library. + return true + end + + return false end --[[