Created
June 10, 2020 15:59
-
-
Save phcorp/5b6c51cda2dfdf49083e7ebccd5815ac to your computer and use it in GitHub Desktop.
Simple php retry
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
<?php | |
declare(strict_types=1); | |
namespace Utils; | |
/** | |
* @param callable $function Function to call | |
* @param int $times Maximum number of times the function is called | |
* | |
* @throws \Exception | |
* | |
* @return mixed $function return | |
*/ | |
function retry(callable $function, int $times = 3) | |
{ | |
do { | |
--$times; | |
try { | |
return $function(); | |
} catch (\Exception $exception) { | |
if (!$times) { | |
throw $exception; | |
} | |
} | |
} while ($times); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment