andeemarks
1/27/2018 - 3:01 AM

lua-exercises-day-2.lua

function retry(count, body) 
    for i = 1, count do
        coro = coroutine.create(body)
        coroutine.resume(coro)
        if (coroutine.status(coro) == "dead") then
            print(i)
            return
        end
    end

    print("Gave up after " .. count .. " attempts")
end

retry(5, 
    function()
        if math.random() > 0.2 then
            coroutine.yield( "Something bad happened" )
        end

        print("Succeeded")
    end
)