-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-async-await.txt
117 lines (83 loc) · 3.27 KB
/
lua-async-await.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
*lua-async-await.txt* For Neovim >= 0.9.4 Last change: 2024 March 31
==============================================================================
Table of Contents *lua-async-await-table-of-contents*
1. lua-async |lua-async-await-lua-async|
- What |lua-async-await-what|
- Why |lua-async-await-why|
- How to use |lua-async-await-how-to-use|
==============================================================================
1. lua-async *lua-async-await-lua-async*
Synchronous like asynchronous for Lua.
WHAT *lua-async-await-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 *lua-async-await-why*
Well, callback creates callback hell.
HOW TO USE *lua-async-await-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
<
Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
vim:tw=78:ts=8:noet:ft=help:norl: