Vapm
  • About
    • Why is it called unblocking?
    • Next update?
    • Credit
    • Info
  • System
    • runEventLoop()
    • runSingleEventLoop()
    • setTimeout()
    • setInterval()
    • fetch()
    • read()
    • time() & timeEnd()
  • Asynchronous
    • getId()
    • await()
  • Promise
    • c()
    • getId()
    • getFiber()
    • isJustGetResult()
    • getTimeOut()
    • getTimeStart()
    • getTimeEnd()
    • setTimeEnd()
    • canDrop()
    • getStatus()
    • getResult()
    • getReturn()
    • getCallback()
    • resolve() & reject()
    • then() & catch() & finally()
    • useCallbacks()
    • all()
    • allSettled()
    • any()
    • race()
    • Some common methods
  • CoroutineGen
    • runBlocking()
    • runNonBlocking()
    • Deferred
    • Deferred Await All
    • repeat() & delay()
    • Channel
      • Example about Channel
    • AwaitGroup
      • Example
    • Mutex
      • Example
    • Goroutine vs CoroutineGen
      • Concurrency
  • Thread
    • How do I create a thread?
    • How do I share data between threads?
    • Advanced Thread
  • Worker
    • Example
  • Stream
    • read()
    • write()
    • append()
    • delete()
    • create()
    • overWrite()
  • FAQ
    • Why System::runEventLoop() and System::runSingleEventLoop() ?
    • Do you want concurrency?
  • PocketMine-PMMP
Powered by GitBook
On this page
  1. CoroutineGen

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
PreviousDeferred Await AllNextChannel

Last updated 9 months ago