Skip to content

Commit

Permalink
add retry helper
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 13, 2016
1 parent 969e1fc commit e3bd359
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,33 @@ function preg_replace_array($pattern, array $replacements, $subject)
}
}

if (! function_exists('retry')) {
/**
* Retry an operation a given number of times.
*
* @param int $times
* @param callable $callback
* @return mixed
*/
function retry($times, callable $callback)
{
$times--;

beginning:
try {
return $callback();
} catch (Exception $e) {
if (! $times) {
throw $e;
}

$times--;

goto beginning;
}
}
}

if (! function_exists('snake_case')) {
/**
* Convert a string to snake case.
Expand Down

2 comments on commit e3bd359

@MartelliEnrico
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is goto any better than recursion or a for-loop?

@neyaoz
Copy link
Contributor

@neyaoz neyaoz commented on e3bd359 Jan 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartelliEnrico It has been already discussed: #15973

Please sign in to comment.