/async-await.lua

Primary LanguageLuaMIT LicenseMIT

async-await.lua

Write async function more like javascript async/await

await the async callback function

local aw = require('async-await')
local timer = vim.loop.new_timer()

local main = aw.async(function ()

  local hello = aw.await(function (cb)
    timer:start(1000, 0, function ()
      cb('hello')
    end)
  end)

  local world = aw.await(function (cb)
    timer:start(1000, 0, function ()
      cb('world')
    end)
  end)

  print(hello, world)
end)

main()

await the async function

local aw = require('async-await')
local timer = vim.loop.new_timer()

local hello_world = aw.async(function ()

  local hello = aw.await(function (cb)
    timer:start(1000, 0, function ()
      cb('hello')
    end)
  end)

  local world = aw.await(function (cb)
    timer:start(1000, 0, function ()
      cb('world')
    end)
  end)

  return hello,world
end)

local main = aw.async(function()
    local hello, world = aw.await(hello_world)
    print(hello, world)
end)

main()