File tree 1 file changed +14
-15
lines changed
1 file changed +14
-15
lines changed Original file line number Diff line number Diff line change 1
1
local path = jsRequire (" path" )
2
2
local fs = jsRequire (" fs" )
3
3
4
- function tick (co )
5
- if coroutine.status (co ) == " suspended" then
6
- coroutine.resume (co )
7
- setImmediate (tick , co )
8
- end
9
- end
10
-
11
4
-- async function to bound awaits
12
5
function async (callback )
13
6
return function (...)
14
- local varargs = {... }
7
+ local co = coroutine.create (callback )
8
+ local safe , result = coroutine.resume (co , ... )
15
9
16
10
return Promise .create (function (resolve , reject )
17
- local co = coroutine.create (function ()
18
- local safe , args = pcall (callback , table.unpack (varargs ))
11
+ local function step ()
12
+ if coroutine.status (co ) == " dead" then
13
+ local send = safe and resolve or reject
14
+ return send (result )
15
+ end
16
+
17
+ safe , result = coroutine.resume (co )
19
18
20
- if safe then
21
- resolve ( args )
19
+ if safe and result == Promise . resolve ( result ) then
20
+ result : finally ( step )
22
21
else
23
- reject ( args )
22
+ step ( )
24
23
end
25
- end )
24
+ end
26
25
27
- tick ( co )
26
+ result : finally ( step )
28
27
end )
29
28
end
30
29
end
You can’t perform that action at this time.
0 commit comments