plenary.nvim icon indicating copy to clipboard operation
plenary.nvim copied to clipboard

How to run nested async functions?

Open mnowotnik opened this issue 3 years ago • 0 comments

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

mnowotnik avatar Aug 09 '22 15:08 mnowotnik