mirror of
https://github.com/danog/amp.git
synced 2024-11-26 20:15:00 +01:00
Update code style in docs
This commit is contained in:
parent
81e7024e14
commit
205d37d849
@ -45,11 +45,11 @@ All `yield`s in a coroutine must be one of the following three types:
|
||||
| `React\Promise\PromiseInterface` | Same as `Amp\Promise`. Any React promise will automatically be adapted to an Amp promise. |
|
||||
| `array` | Yielding an array of promises combines them implicitly using `Amp\Promise\all()`. An array with elements not being promises will result in an `Amp\InvalidYieldError`. |
|
||||
|
||||
## Yield VS Yield From
|
||||
## Yield vs. Yield From
|
||||
|
||||
Yield is used to "await" promises, yield from can be used to delegate to a sub-routine. Yield from should only be used to delegate to private methods, any public API should always return promises instead of generators.
|
||||
`yield` is used to "await" promises, `yield from` can be used to delegate to a sub-routine. `yield from` should only be used to delegate to private methods, any public API should always return promises instead of generators.
|
||||
|
||||
Yield promises from within a `\Generator`, the code within the `\Generator` will continue, as soon as the promise is resolved. Use `yield from` to yield the result of a `\Generator`. Instead of using yield from, you can also use `yield new Coroutine($this->bar());` or `yield call([$this, "bar"]);`.
|
||||
When a promise is yielded from within a `\Generator`, `\Generator` will be paused and continue as soon as the promise is resolved. Use `yield from` to yield another `\Generator`. Instead of using `yield from`, you can also use `yield new Coroutine($this->bar());` or `yield call([$this, "bar"]);`.
|
||||
|
||||
An example:
|
||||
|
||||
@ -99,4 +99,4 @@ int(3)
|
||||
int(3)
|
||||
```
|
||||
|
||||
For further information about the "yield from" keyword, see [PHP Manual](http://php.net/manual/en/language.generators.syntax.php#control-structures.yield.from)
|
||||
For further information about `yield from`, consult the [PHP manual](http://php.net/manual/en/language.generators.syntax.php#control-structures.yield.from).
|
||||
|
@ -83,7 +83,8 @@ use Amp\Loop;
|
||||
|
||||
$myText = null;
|
||||
|
||||
function onInput($watcherId, $stream) {
|
||||
function onInput($watcherId, $stream)
|
||||
{
|
||||
global $myText;
|
||||
|
||||
$myText = fgets($stream);
|
||||
|
@ -221,15 +221,18 @@ For a slightly more complex use case, let's look at a common scenario where a se
|
||||
|
||||
use Amp\Loop;
|
||||
|
||||
class Server {
|
||||
class Server
|
||||
{
|
||||
private $clients = [];
|
||||
|
||||
public function startServer() {
|
||||
public function startServer()
|
||||
{
|
||||
// ... server bind and accept logic would exist here
|
||||
Loop::run();
|
||||
}
|
||||
|
||||
private function onNewClient($sock) {
|
||||
private function onNewClient($sock)
|
||||
{
|
||||
$socketId = (int) $sock;
|
||||
$client = new ClientStruct;
|
||||
$client->socket = $sock;
|
||||
@ -250,12 +253,14 @@ class Server {
|
||||
|
||||
// ... other class implementation details here ...
|
||||
|
||||
private function writeToClient($client, $data) {
|
||||
private function writeToClient($client, $data)
|
||||
{
|
||||
$client->writeBuffer .= $data;
|
||||
$this->doWrite($client);
|
||||
}
|
||||
|
||||
private function doWrite(ClientStruct $client) {
|
||||
private function doWrite(ClientStruct $client)
|
||||
{
|
||||
$bytesToWrite = strlen($client->writeBuffer);
|
||||
$bytesWritten = @fwrite($client->socket, $client->writeBuffer);
|
||||
|
||||
|
@ -12,7 +12,8 @@ Every iterator in Amp follows the `Amp\Iterator` interface.
|
||||
```php
|
||||
namespace Amp;
|
||||
|
||||
interface Iterator {
|
||||
interface Iterator
|
||||
{
|
||||
public function advance(): Promise;
|
||||
public function getCurrent();
|
||||
}
|
||||
|
@ -117,7 +117,8 @@ Sometimes values are immediately available. This might be due to them being cach
|
||||
`Amp\Deferred` is the abstraction responsible for resolving future values once they become available. A library that resolves values asynchronously creates an `Amp\Deferred` and uses it to return an `Amp\Promise` to API consumers. Once the async library determines that the value is ready it resolves the promise held by the API consumer using methods on the linked promisor.
|
||||
|
||||
```php
|
||||
final class Deferred {
|
||||
final class Deferred
|
||||
{
|
||||
public function promise(): Promise;
|
||||
public function resolve($result = null);
|
||||
public function fail(Throwable $error);
|
||||
@ -143,7 +144,8 @@ Here's a simple example of an async value producer `asyncMultiply()` creating a
|
||||
|
||||
use Amp\Loop;
|
||||
|
||||
function asyncMultiply($x, $y) {
|
||||
function asyncMultiply($x, $y)
|
||||
{
|
||||
// Create a new promisor
|
||||
$deferred = new Amp\Deferred;
|
||||
|
||||
@ -157,5 +159,6 @@ function asyncMultiply($x, $y) {
|
||||
|
||||
$promise = asyncMultiply(6, 7);
|
||||
$result = Amp\Promise\wait($promise);
|
||||
|
||||
var_dump($result); // int(42)
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user