Skip to content

Commit c8d9dd1

Browse files
chore(doc): automatic vimdoc update (#3)
Co-authored-by: s1n7ax <[email protected]>
1 parent c268e8f commit c8d9dd1

File tree

1 file changed

+66
-84
lines changed

1 file changed

+66
-84
lines changed

doc/lua-async-await.txt

+66-84
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,115 @@
1-
*lua-async-await.txt* For Neovim >= 0.9.4 Last change: 2023 December 10
1+
*lua-async-await.txt* For Neovim >= 0.9.4 Last change: 2024 March 31
22

33
==============================================================================
44
Table of Contents *lua-async-await-table-of-contents*
55

6-
1. Lua Async Await |lua-async-await-lua-async-await|
7-
- Why? |lua-async-await-why?|
6+
1. Lua Async |lua-async-await-lua-async|
7+
- What |lua-async-await-what|
8+
- Why |lua-async-await-why|
89
- How to use |lua-async-await-how-to-use|
910

1011
==============================================================================
11-
1. Lua Async Await *lua-async-await-lua-async-await*
12+
1. Lua Async *lua-async-await-lua-async*
1213

13-
This is basically ms-jpq/lua-async-await
14-
<https://github.com./ms-jpq/lua-async-await> but with Promise like error
15-
handling
14+
Synchronous like asynchronous for Lua.
1615

17-
Refer the original repository for more comprehensive documentation on how all
18-
this works
1916

17+
WHAT *lua-async-await-what*
2018

21-
WHY? *lua-async-await-why?*
19+
Take a look at before and after
2220

23-
A Language Server command response contains two parameters. `error` &
24-
`response`. If the error is present then the error should be handled.
25-
26-
Ex:-
21+
**Before:**
2722

2823
>lua
29-
self.client.request('workspace/executeCommand', cmd_info, function(err, res)
24+
request('workspace/executeCommand', cmd_info, function(err, res)
3025
if err then
31-
log.error(command .. ' failed! arguments: ', arguments, ' error: ', err)
26+
log.error(err)
3227
else
33-
log.debug(command .. ' success! response: ', res)
28+
log.debug(res)
3429
end
3530
end, buffer)
3631
<
3732

38-
Promises are fine but chaining is annoying specially when you don’t have
39-
arrow function like syntactic sugar. Moreover, at the time of this is writing,
40-
Lua language server generics typing is so primitive and cannot handle
41-
`Promise<Something>` like types.
33+
**After:**
34+
35+
>lua
36+
-- on error, statement will fail throwing an error just like any synchronous API
37+
local result = request('workspace/executeCommand', cmd_info, buffer)
38+
log.debug(result)
39+
<
40+
4241

43-
So I wanted Promise like error handling but without Promises.
42+
WHY *lua-async-await-why*
4443

44+
Well, callback creates callback hell.
4545

46-
HOW TO USE *lua-async-await-how-to-use*
4746

48-
Assume following is the asynchronous API
47+
HOW TO USE *lua-async-await-how-to-use*
4948

5049
>lua
51-
local function lsp_request(callback)
50+
local runner = require("async.runner")
51+
local wrap = require("async.wrap")
52+
local wait = require("async.waits.wait_with_error_handler")
53+
54+
local function success_async(callback)
5255
local timer = vim.loop.new_timer()
5356

5457
assert(timer)
5558

5659
timer:start(2000, 0, function()
5760
-- First parameter is the error
58-
callback('something went wrong', nil)
61+
callback(nil, "hello world")
5962
end)
6063
end
61-
<
62-
63-
64-
WHEN NO ERROR HANDLER DEFINED ~
65-
66-
This is how you can call this asynchronous API without a callback
67-
68-
>lua
69-
local M = require('sync')
7064

71-
M.sync(function()
72-
local response = M.wait_handle_error(M.wrap(lsp_request)())
73-
end).run()
74-
<
75-
76-
Result:
77-
78-
>
79-
Error executing luv callback:
80-
test6.lua:43: unhandled error test6.lua:105: something went wrong
81-
stack traceback:
82-
[C]: in function 'error'
83-
test6.lua:43: in function 'callback'
84-
test6.lua:130: in function <test6.lua:129>
85-
<
86-
87-
88-
WHEN ERROR HANDLER IS DEFINED ~
89-
90-
>lua
91-
local M = require('sync')
65+
local function fail_async(callback)
66+
local timer = vim.loop.new_timer()
9267

93-
local main = M.sync(function()
94-
local response = M.wait_handle_error(M.wrap(lsp_request)())
95-
end)
96-
.catch(function(err)
97-
print('error occurred ', err)
68+
assert(timer)
69+
70+
timer:start(2000, 0, function()
71+
-- First parameter is the error
72+
callback("something went wrong", nil)
9873
end)
99-
.run()
100-
<
101-
102-
Result:
103-
104-
>
105-
error occurred test6.lua:105: something went wrong
106-
<
107-
108-
109-
WHEN NESTED ~
110-
111-
>lua
112-
local M = require('sync')
74+
end
11375

114-
local nested = M.sync(function()
115-
local response = M.wait_handle_error(M.wrap(lsp_request)())
116-
end)
76+
local function log(message)
77+
vim.print(os.date("%H:%M:%S") .. " " .. message)
78+
end
79+
80+
vim.cmd.messages("clear")
11781

118-
M.sync(function()
119-
M.wait_handle_error(nested.run)
82+
local nested = runner(function()
83+
local success_sync = wrap(success_async)
84+
local fail_sync = wrap(fail_async)
85+
86+
local success_result = wait(success_sync())
87+
-- here we get the result because there is no error
88+
log("success_result is: " .. success_result)
89+
90+
-- following is going to fail and error will get caught by
91+
-- the parent runner function's 'catch'
92+
wait(fail_sync())
12093
end)
94+
95+
runner(function()
96+
log("starting the execution")
97+
-- just wait for nested runner to complete the execution
98+
wait(nested.run)
99+
end)
121100
.catch(function(err)
122-
print('parent error handler ' .. err)
101+
log("parent error handler " .. err)
123102
end)
124103
.run()
125104
<
126105

127-
Result:
128106

129-
>
130-
parent error handler test6.lua:105: test6.lua:105: something went wrong
107+
OUTPUT ~
108+
109+
>txt
110+
18:44:46 starting the execution
111+
18:44:48 success_result is: hello world
112+
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
131113
<
132114

133115
Generated by panvimdoc <https://github.com./kdheepak/panvimdoc>

0 commit comments

Comments
 (0)