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

How to run nested async functions? #395

Open
mnowotnik opened this issue Aug 9, 2022 · 0 comments
Open

How to run nested async functions? #395

mnowotnik opened this issue Aug 9, 2022 · 0 comments

Comments

@mnowotnik
Copy link
Contributor

I'm having trouble figuring out how I can nest async.wrap function calls.

What seems to be working is:

local leaf1 = async.wrap(function(cb)
  cb("leaf1")
end, 1)

local leaf2 = async.wrap(function(cb)
  cb("leaf2")
end, 1)

local branch = async.wrap(function(cb)
  local l1,l2
  async.run(leaf1, function(val)
    l1 = val
  end)
  async.run(leaf2, function(val)
    l2 = val
  end)
  cb("branch" .. l1 .. l2)
end, 1)

function run()
    local root = async.void(function()
        print(leaf1())
        print(leaf2())
        print(branch())
    end)
    root()
end

run()

I expected plain function calls to work, but they crash with an error:

local leaf1 = async.wrap(function(cb)
  cb("leaf1")
end, 1)

local leaf2 = async.wrap(function(cb)
  cb("leaf2")
end, 1)

local branch = async.wrap(function(cb)
  local l1,l2
  l1 = leaf1()
  l2 = leaf2()
  cb("branch" .. l1 .. l2)
end, 1)

M.async = function()
    local root = async.void(function()
        print(leaf1())
        print(leaf2())
        print(branch())
    end)
    root()
end

Error:

 attempt to yield across C-call boundary
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