Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

nvim-java/lua-async

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-async

Synchronous like asynchronous for Lua.

What

Take a look at before and after

Before:

request('workspace/executeCommand', cmd_info, function(err, res)
  if err then
    log.error(err)
  else
    log.debug(res)
  end
end, buffer)

After:

-- on error, statement will fail throwing an error just like any synchronous API
local result  = request('workspace/executeCommand', cmd_info, buffer)
log.debug(result)

Why

Well, callback creates callback hell.

How to use

local runner = require("async.runner")
local wrap = require("async.wrap")
local wait = require("async.waits.wait_with_error_handler")

local function success_async(callback)
  local timer = vim.loop.new_timer()

  assert(timer)

  timer:start(2000, 0, function()
    -- First parameter is the error
    callback(nil, "hello world")
  end)
end

local function fail_async(callback)
  local timer = vim.loop.new_timer()

  assert(timer)

  timer:start(2000, 0, function()
    -- First parameter is the error
    callback("something went wrong", nil)
  end)
end

local function log(message)
  vim.print(os.date("%H:%M:%S") .. " " .. message)
end

vim.cmd.messages("clear")

local nested = runner(function()
  local success_sync = wrap(success_async)
  local fail_sync = wrap(fail_async)

  local success_result = wait(success_sync())
  -- here we get the result because there is no error
  log("success_result is: " .. success_result)

  -- following is going to fail and error will get caught by
  -- the parent runner function's 'catch'
  wait(fail_sync())
end)

runner(function()
    log("starting the execution")
    -- just wait for nested runner to complete the execution
    wait(nested.run)
  end)
  .catch(function(err)
    log("parent error handler " .. err)
  end)
  .run()

Output

18:44:46 starting the execution
18:44:48 success_result is: hello world
18:44:50 parent error handler ...-async-await/lua/async/waits/wait_with_error_handler.lua:14: ...-async-await/lua/async/waits/wait_with_error_handler.lua:14: something went wrong

About

Synchronous like asynchronous with promise like error handling

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.