How to run nested async functions?

Open
#395 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
lua
Domain
tooling

Research direction

Start by reproducing the nested calls in the issue, then inspect the implementations or entry points for async.wrap, async.run, and async.void. The reported failure is an attempt to yield across a C-call boundary; done means establishing supported nested-call behavior or documenting the required calling pattern.

Written by the indexing model from the issue text.

Description

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
Dominant language
Lua
Stars
3.5k
Forks
340
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nvim-lua/plenary.nvim

All issues in nvim-lua/plenary.nvim

Similar issues

More Lua issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.