Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support tables with __call metamethod in handlers #64

Closed
matthargett opened this issue Jul 4, 2021 · 1 comment
Closed

support tables with __call metamethod in handlers #64

matthargett opened this issue Jul 4, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@matthargett
Copy link

The library currently checks for:

	assert(
		successHandler == nil or type(successHandler) == "function",
		string.format(ERROR_NON_FUNCTION, "Promise:andThen")
	)
	assert(
		failureHandler == nil or type(failureHandler) == "function",
		string.format(ERROR_NON_FUNCTION, "Promise:andThen")
	)

which will fail for callable tables that use the __call metamethod, so this code fails the assertion above:

		local wrapped = {}
		setmetatable(wrapped, {
			__call = _wrapped,
		})
		wrapped.cancel = _cancel

                Promise.new():andThen(wrapped, wrapped)

a helper method that could be used in the library might look like:

local function isCallable(value)
  if typeof(value) == "function" then
    return true
  end
  if typeof(value) == "table" then
    local mt = getmetatable(value)
    if mt and rawget(mt, "__call") then
      return true
    end
  end
  return false
end
@evaera evaera added the enhancement New feature or request label Jul 7, 2021
@evaera
Copy link
Owner

evaera commented Jul 7, 2021

Supporting callable tables sounds good to me. We should make sure that we update this behavior everywhere we check for things that are functions.

@evaera evaera closed this as completed in 2943b69 Dec 28, 2021
evaera added a commit that referenced this issue Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants