# Deferred

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 [**`Generator`**](https://www.php.net/manual/en/language.generators.php) callback.

```php
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" />
...."
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://venndev.gitbook.io/vapm/coroutinegen/deferred.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
