-
Notifications
You must be signed in to change notification settings - Fork 11
/
await.lua
51 lines (36 loc) · 1.08 KB
/
await.lua
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
-- from incredible-gmod.ru with <3
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/await.lua
function await(fn, ...)
local co = coroutine.running()
fn(function(succ, err, ...)
if succ == false then
ErrorNoHaltWithStack(err .."\n")
end
coroutine.resume(co, err, ...)
end, ...)
return coroutine.yield()
end
function fetch(cback, url)
http.Fetch(url, function(body, len, headers, code)
cback(true, body, len, headers, code)
end, function(err)
cback(false, err)
end)
end
local response = await(fetch, "https://google.com/")
-- if you doesnt wont to design your sync functions
function await(fn, cback_pos, ...)
local co = coroutine.running()
local args = {...}
if cback_pos then
for i = cback_pos, #args do
args[i + 1], args[i] = args[i], nil
end
end
args[cback_pos or #args + 1] = function(...)
coroutine.resume(co, ...)
end
fn(unpack(args))
return coroutine.yield()
end
local response = await(http.Fetch, 2, "https://google.com/")