repeat() & delay()
/**
* @param callable $callback
* @param int $times
* @return Closure
*
* This is a generator that runs a callback function a specified amount of times.
*/
public static function repeat(callable $callback, int $times) : Closure;
/**
* @param int $milliseconds
* @return Generator
*
* This is a generator that yields for a specified amount of milliseconds.
*/
public static function delay(int $milliseconds) : Generator;
These are 2 functions that should only be used in a Coroutine Scope or a runBlocking().
CoroutineGen::runNonBlocking(
function() : Generator {
yield from CoroutineGen::delay(3000);
var_dump("A");
},
CoroutineGen::repeat(function() {
yield from CoroutineGen::delay(100);
var_dump("B");
}, 5),
function() {
var_dump("C");
},
function() : Generator {
yield from CoroutineGen::delay(3000);
var_dump("D");
},
function() {
var_dump("E");
}
);
Output:
string(1) "C"
string(1) "E"
string(1) "B"
string(1) "B"
string(1) "B"
string(1) "B"
string(1) "B"
string(1) "A"
string(1) "D"
Time for Console: 6.2208368778229
Last updated