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

[Proposal] Convenient one-shot async call of function #20

Open
yioneko opened this issue May 31, 2024 · 0 comments
Open

[Proposal] Convenient one-shot async call of function #20

yioneko opened this issue May 31, 2024 · 0 comments

Comments

@yioneko
Copy link

yioneko commented May 31, 2024

I don't know if this type of API was ever been considered, but I'd like to see this form of async call API:

local nio = require("nio")
local function func_with_cb(arg1, arg2, cb)
  -- ....
end
-- currently, to call one function asynchronously, we have to firstly `nio.wrap` it:
local async_func = nio.wrap(func_with_cb, 2)
local ret = async_func(arg1, arg2)

-- while how about we have this form of one-shot call API:
local ret = nio.call(func_with_cb, arg1, arg2)
-- In this form, not only we do not need to wrap it first, but also there is
-- no assumption on the argc of function, and we do not need to count
-- argc manually

-- The API could be defined as simple as follows:
function nio.call(func, ...)
  local argc = select("#", ...) + 1
  local args = { ... }
  local ret = { coroutine.yield(argc, func, ...) }
  return unpack(ret, 2, table.maxn(ret))
end

Also I wonder if it is possible to make argc optional in nio.wrap by using debug.getinfo(func, "u").nparams.

EDIT: My attempt to implement these: yioneko@f057066

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant