Skip to content

Commit

Permalink
improve error-handling in async function
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
BuckarooBanzay committed Jul 31, 2024
1 parent 61a51ec commit 526c2bc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
8 changes: 1 addition & 7 deletions async.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

local err_symbol = {}

local function await(p)
assert(coroutine.running(), "running inside a Promise.async() call")
local result = nil
Expand All @@ -18,7 +16,7 @@ local function await(p)
while true do
if finished then
if err then
return coroutine.yield({ err = err, err_symbol = err_symbol })
return nil, err
else
return unpack(result)
end
Expand All @@ -42,10 +40,6 @@ function Promise.async(fn)
if not cont then
-- error in first async() level
p:reject(result)
end
if type(result) == "table" and result.err_symbol == err_symbol then
-- error in await() call
p:reject(result.err)
return
end
minetest.after(0, step)
Expand Down
20 changes: 10 additions & 10 deletions async.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ end)

mtt.register("Promise.async with handle_async", function()
return Promise.async(function(await)
local v = await(Promise.resolved(42))
local v, err = await(Promise.resolved(42))
assert(v == 42)
v = await(Promise.handle_async(function() return 100 end))
assert(not err)
v, err = await(Promise.handle_async(function() return 100 end))
assert(v == 100)
assert(not err)
end)
end)

mtt.register("Promise.async rejected", function(callback)
local p = Promise.async(function(await)
await(Promise.rejected("my-err"))
mtt.register("Promise.async error propagation", function()
return Promise.async(function(await)
local v, err = await(Promise.rejected("my-err"))
assert(not v)
assert(err)
end)

p:catch(function(e)
assert(type(e) == "string")
callback()
end)
end)

mtt.register("Promise.async error", function(callback)
mtt.register("Promise.async direct error", function(callback)
local p = Promise.async(function()
error("stuff")
end)
Expand Down

0 comments on commit 526c2bc

Please sign in to comment.