Skip to content

Commit aca717c

Browse files
ms-jpqs1n7ax
authored andcommitted
now able to return funcs
1 parent 37999f2 commit aca717c

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Async Await in [90 lines](https://github.com/ms-jpq/neovim-async-tutorial/blob/m
44

55
## Preface
66

7+
### Special Thanks
8+
9+
[svermeulen](https://github.com/svermeulen) for fixing [inability to return functions](https://github.com/ms-jpq/neovim-async-tutorial/issues/2).
10+
11+
712
This tutorial assumes that you are familiar with the concept of `async` `await`
813

914
You will also need to read through the [first 500 words](https://www.lua.org/pil/9.1.html) of how coroutines work in lua.

lua/async.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ local pong = function (func, callback)
1111
local thread = co.create(func)
1212
local step = nil
1313
step = function (...)
14-
local go, ret = co.resume(thread, ...)
15-
if not go then
16-
assert(co.status(thread) == "suspended", ret)
17-
elseif type(ret) == "function" then
18-
ret(step)
19-
else
14+
local stat, ret = co.resume(thread, ...)
15+
assert(stat, ret)
16+
if co.status(thread) == "dead" then
2017
(callback or function () end)(ret)
18+
else
19+
assert(type(ret) == "function", "type error :: expected func")
20+
ret(step)
2121
end
2222
end
2323
step()
@@ -84,4 +84,3 @@ return {
8484
wait_all = await_all,
8585
wrap = wrap,
8686
}
87-

lua/tutorial.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ local async_tasks_1 = function()
7272
end)
7373
end
7474

75+
7576
local async_tasks_2 = function (val)
7677
return a.sync(function ()
7778
-- await all
7879
local w, z = a.wait_all{e2(val, val + 1), e2(val + 2, val + 3)}
7980
print(unpack(w))
8081
print(unpack(z))
81-
return 4
82+
return function ()
83+
return 4
84+
end
8285
end)
8386
end
8487

@@ -88,7 +91,7 @@ local async_example = function ()
8891
-- composable, await other async thunks
8992
local u = a.wait(async_tasks_1())
9093
local v = a.wait(async_tasks_2(3))
91-
print(u + v)
94+
print(u + v())
9295
end)
9396
end
9497

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy