Skip to content

Cannot return functions in async methods #2

Closed
@svermeulen

Description

@svermeulen

Using the implementation here, you cannot create async functions that return functions, since the returned functions will always be interpreted as a 'thunk'. I attempted to modify the pong method to allow this. In case it's useful to others, I wanted to share what I'm using instead:

local pong = function (func, callback)
  assert(type(func) == "function", "Expected type func but found " .. type(func))
  local thread = co.create(func)
  local step = nil
  step = function (...)
    local go, ret = co.resume(thread, ...)
    assert(go)
    if co.status(thread) == "dead" then
      if callback then
        callback(ret)
      end
    else
      assert(type(ret) == "function")
      ret(step)
    end
  end
  step()
end

This allows function return values because it always calls the callback value when the coroutine is dead. Also, I think we can assume that the go value is always true now that we are checking the co.status immediately after the call to resume

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions