while (!$success) { tryAgain(); }

1.0.0 2014-05-30 06:05 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:02:44 UTC


README

Retry 是一个有用的类,允许您重试任务直到它们成功,可以选择在迭代之间设置延迟,包括灵活的条件和可读的语法。

RETRY

用法

// This retries running the given function until "working" is returned. It retries
// up to ten times, pausing for 1000 ms (one second) in between attempts.

Retry::running(function () {
    if (doSomething()) {
        return 'working';
    } else {
        return 'broken';
    }
})->whileIsnt('working')
  ->delay(1000)
  ->go(10);

// Passing a callable in "soLongAs" allows you to transform the output before
// testing its value. Retry will also return the last value gotten from
// running(), if it did ever succeed, or the boolean false if it did not.

$arrayThing = Retry::running(function () {
    return complicatedArrayThing();
})->soLongAs(function ($arrayThing) {
    return !! $arrayThing;
})->is(false)->go(5);

if ($arrayThing === false) {
    echo "We're out of ArrayThings today!";
} else {
    echo "Here's your ArrayThing!";
    var_dump($arrayThing);
}