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

Deferred

PreviousrunNonBlocking()NextDeferred Await All

Last updated 9 months ago

Similar to a Deferred of Kotlin carries a function like await() to return a result when the waiting task is completed.

Keep in mind that when using the Deferred class, you need to specify that it is a callback.

CoroutineGen::runNonBlocking(
    function() : Generator {
        $deferred = new Deferred(function() : Generator {
            return yield file_get_contents('http://www.weather.com.cn/data/cityinfo/101270101.html');
        });
        
        $data = yield from $deferred->await();
        var_dump($data);
    },
    function() : Generator {
        yield var_dump("END!");
    },
    function() {
        var_dump("START!");
    },
    function() {
        var_dump("Hello World");
    }
);

Output:

string(6) "START!"
string(11) "Hello World"
string(4) "END!"
string(10989) "<!DOCTYPE HTML>
<html>
<head>
<link rel="dns-prefetch" href="http://i.tq121.com.cn">
<meta charset="utf-8" />
...."
Generator