https://github.com/nvim-java/lua-async
Synchronous like asynchronous with promise like error handling
https://github.com/nvim-java/lua-async
async async-await asynchronous await-async coroutines lua synchronous
Last synced: 4 months ago
JSON representation
Synchronous like asynchronous with promise like error handling
- Host: GitHub
- URL: https://github.com/nvim-java/lua-async
- Owner: nvim-java
- License: mit
- Created: 2023-12-10T18:31:05.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-31T13:33:02.000Z (almost 2 years ago)
- Last Synced: 2024-04-01T14:25:36.432Z (almost 2 years ago)
- Topics: async, async-await, asynchronous, await-async, coroutines, lua, synchronous
- Language: Lua
- Homepage:
- Size: 40 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# lua-async
Synchronous like asynchronous for Lua.
## What
Take a look at before and after
**Before:**
```lua
request('workspace/executeCommand', cmd_info, function(err, res)
if err then
log.error(err)
else
log.debug(res)
end
end, buffer)
```
**After:**
```lua
-- 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
```lua
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
```txt
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
```