Skip to content

Commit 5ab4f43

Browse files
authored
fix(async): consider metatable case when checking if callable (#396)
1 parent a3dafaa commit 5ab4f43

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

lua/plenary/async/async.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ local f = require "plenary.functional"
66

77
local M = {}
88

9+
local function is_callable(fn)
10+
return type(fn) == "function" or type(getmetatable(fn)["__call"]) == "function"
11+
end
12+
913
---because we can't store varargs
1014
local function callback_or_next(step, thread, callback, ...)
1115
local stat = f.first(...)
@@ -22,7 +26,8 @@ local function callback_or_next(step, thread, callback, ...)
2226
else
2327
local returned_function = f.second(...)
2428
local nargs = f.third(...)
25-
assert(type(returned_function) == "function", "type error :: expected func")
29+
30+
assert(is_callable(returned_function), "type error :: expected func")
2631
returned_function(vararg.rotate(nargs, step, select(4, ...)))
2732
end
2833
end
@@ -31,7 +36,7 @@ end
3136
---@param async_function Future: the future to execute
3237
---@param callback function: the callback to call when done
3338
local execute = function(async_function, callback, ...)
34-
assert(type(async_function) == "function", "type error :: expected func")
39+
assert(is_callable(async_function), "type error :: expected func")
3540

3641
local thread = co.create(async_function)
3742

@@ -69,7 +74,7 @@ end
6974
---@param argc number: The number of arguments of func. Must be included.
7075
---@return function: Returns an async function
7176
M.wrap = function(func, argc)
72-
if type(func) ~= "function" then
77+
if not is_callable(func) then
7378
traceback_error("type error :: expected func, got " .. type(func))
7479
end
7580

0 commit comments

Comments
 (0)