Skip to content

Commit e069e5a

Browse files
chore(doc): automatic vimdoc update (#2)
Co-authored-by: s1n7ax <s1n7ax@users.noreply.github.com>
1 parent cd81626 commit e069e5a

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

doc/lua-async-await.txt

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
*lua-async-await.txt* For Neovim >= 0.9.4 Last change: 2023 December 10
2+
3+
==============================================================================
4+
Table of Contents *lua-async-await-table-of-contents*
5+
6+
1. Lua Async Await |lua-async-await-lua-async-await|
7+
- Why? |lua-async-await-why?|
8+
- How to use |lua-async-await-how-to-use|
9+
10+
==============================================================================
11+
1. Lua Async Await *lua-async-await-lua-async-await*
12+
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
16+
17+
Refer the original repository for more comprehensive documentation on how all
18+
this works
19+
20+
21+
WHY? *lua-async-await-why?*
22+
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:-
27+
28+
>lua
29+
self.client.request('workspace/executeCommand', cmd_info, function(err, res)
30+
if err then
31+
log.error(command .. ' failed! arguments: ', arguments, ' error: ', err)
32+
else
33+
log.debug(command .. ' success! response: ', res)
34+
end
35+
end, buffer)
36+
<
37+
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.
42+
43+
So I wanted Promise like error handling but without Promises.
44+
45+
46+
HOW TO USE *lua-async-await-how-to-use*
47+
48+
Assume following is the asynchronous API
49+
50+
>lua
51+
local function lsp_request(callback)
52+
local timer = vim.loop.new_timer()
53+
54+
assert(timer)
55+
56+
timer:start(2000, 0, function()
57+
-- First parameter is the error
58+
callback('something went wrong', nil)
59+
end)
60+
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')
70+
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')
92+
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)
98+
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')
113+
114+
local nested = M.sync(function()
115+
local response = M.wait_handle_error(M.wrap(lsp_request)())
116+
end)
117+
118+
M.sync(function()
119+
M.wait_handle_error(nested.run)
120+
end)
121+
.catch(function(err)
122+
print('parent error handler ' .. err)
123+
end)
124+
.run()
125+
<
126+
127+
Result:
128+
129+
>
130+
parent error handler test6.lua:105: test6.lua:105: something went wrong
131+
<
132+
133+
Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
134+
135+
vim:tw=78:ts=8:noet:ft=help:norl:

0 commit comments

Comments
 (0)