5 min read

Retry

Attempt to execute a callback until it succeeds or the maximum retry threshold is met and exception is thrown.

           retry($times, $callback, $sleepMilliseconds, $when)
        
           $valueOfResult = retry(
     times: 10, // max number of retries before throwing exception
     callback: function(){
         // get result from API or other error-prone operation
         // if successful returns the value to the caller
         return $result;
     })
     sleepMilliseconds: 50, // try again after 50ms
     when: function($exception) {
         // dynamically decide if we should retry based on the exception
         // return true if you want to retry
         return true
     }