src/Code/Code.php
public static function spin($callable, $timeout = 0, $delay = 100000)
    {
        if (!is_callable($callable)) {
            throw new InvalidArgumentException();
        }

        $closure = function () use ($callable, $timeout, $delay) {
            $timeout = (float) $timeout;
            $start = microtime(true);

            do {
                if ($result = $callable()) {
                    return $result;
                }
                usleep($delay);
                $current = microtime(true);
            } while ($current - $start < $timeout);

            throw new TimeoutException("Timeout reached, execution aborted after {$timeout} second(s).");
        };

        if (!function_exists('pcntl_signal')) {
            return $closure();
        }
        return static::run($closure, $timeout);
    }
public static function spin($callable, $timeout = 0, $delay = 100000)
    {
        if (!is_callable($callable)) {
            throw new InvalidArgumentException();
        }

        $closure = function () use ($callable, $timeout, $delay) {
            $timeout = (float) $timeout;
            $start = microtime(true);

            do {
                if ($result = $callable()) {
                    return $result;
                }
                usleep($delay);
                $current = microtime(true);
            } while ($current - $start < $timeout);

            throw new TimeoutException("Timeout reached, execution aborted after {$timeout} second(s).");
        };

        if (!function_exists('pcntl_signal')) {
            return $closure();
        }
        return static::run($closure, $timeout);
    }
public static function spin ( $ callable , $ timeout = 0 , $ delay = 100000 ) { if ( ! is_callable ( $ callable ) ) { throw new InvalidArgumentException ( ) ; } $ closure = function ( ) use ( $ callable , $ timeout , $ delay ) { $ timeout = ( float ) $ timeout ; $ start = microtime ( true ) ; do { if ( $ result = $ callable ( ) ) { return $ result ; } usleep ( $ delay ) ; $ current = microtime ( true ) ; } while ( $ current - $ start < $ timeout ) ; throw new TimeoutException ( "Timeout reached, execution aborted after {$timeout} second(s)." ) ; } ; if ( ! function_exists ( 'pcntl_signal' ) ) { return $ closure ( ) ; } return static :: run ( $ closure , $ timeout ) ; }
Executes a callable in a loop until a timeout is reached or the callable returns `true`.

@param  Callable $callable The callable to execute.
@param  integer  $timeout  The timeout value.
@return mixed
Executes a callable in a loop until a timeout is reached or the callable returns true .