Good Code
The good version checks whether the coroutine is already dead and keeps the ok flag from resume. Errors return as error values, not normal task output.
Lesson 05
Check coroutine status and resume results so yielded work cannot fail silently.
local function resume_worker(worker)
if coroutine.status(worker) == "dead" then
return nil, "worker already finished"
end
-- resume returns success separately from yielded values.
local ok, result = coroutine.resume(worker)
if not ok then
return nil, result
end
return result
endlocal function resume_worker(worker)
-- The error value is treated like normal work output.
local _, result = coroutine.resume(worker)
return result
endThe good version checks whether the coroutine is already dead and keeps the ok flag from resume. Errors return as error values, not normal task output.
The bad version ignores the success flag. A runtime error inside the coroutine can look like a normal yielded result.