Last active
January 27, 2018 11:18
-
-
Save andeemarks/535a577838f874875ba619f9ca6f024d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment