Skip to content

Commit 526c2bc

Browse files
improve error-handling in async function
fixes #3
1 parent 61a51ec commit 526c2bc

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

async.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11

2-
local err_symbol = {}
3-
42
local function await(p)
53
assert(coroutine.running(), "running inside a Promise.async() call")
64
local result = nil
@@ -18,7 +16,7 @@ local function await(p)
1816
while true do
1917
if finished then
2018
if err then
21-
return coroutine.yield({ err = err, err_symbol = err_symbol })
19+
return nil, err
2220
else
2321
return unpack(result)
2422
end
@@ -42,10 +40,6 @@ function Promise.async(fn)
4240
if not cont then
4341
-- error in first async() level
4442
p:reject(result)
45-
end
46-
if type(result) == "table" and result.err_symbol == err_symbol then
47-
-- error in await() call
48-
p:reject(result.err)
4943
return
5044
end
5145
minetest.after(0, step)

async.spec.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ end)
3232

3333
mtt.register("Promise.async with handle_async", function()
3434
return Promise.async(function(await)
35-
local v = await(Promise.resolved(42))
35+
local v, err = await(Promise.resolved(42))
3636
assert(v == 42)
37-
v = await(Promise.handle_async(function() return 100 end))
37+
assert(not err)
38+
v, err = await(Promise.handle_async(function() return 100 end))
3839
assert(v == 100)
40+
assert(not err)
3941
end)
4042
end)
4143

42-
mtt.register("Promise.async rejected", function(callback)
43-
local p = Promise.async(function(await)
44-
await(Promise.rejected("my-err"))
44+
mtt.register("Promise.async error propagation", function()
45+
return Promise.async(function(await)
46+
local v, err = await(Promise.rejected("my-err"))
47+
assert(not v)
48+
assert(err)
4549
end)
4650

47-
p:catch(function(e)
48-
assert(type(e) == "string")
49-
callback()
50-
end)
5151
end)
5252

53-
mtt.register("Promise.async error", function(callback)
53+
mtt.register("Promise.async direct error", function(callback)
5454
local p = Promise.async(function()
5555
error("stuff")
5656
end)

0 commit comments

Comments
 (0)