Skip to content

Commit

Permalink
Merge pull request #57 from clue-labs/docs-examples
Browse files Browse the repository at this point in the history
Improve examples for `async()` and `await()`
  • Loading branch information
WyriHaximus authored Jul 6, 2022
2 parents 257634a + f5d8b97 commit 4dae336
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ will still be blocked, but everything outside this function can be executed
asynchronously without blocking:

```php
Loop::addTimer(0.5, React\Async\async(function() {
Loop::addTimer(0.5, React\Async\async(function () {
echo 'a';
React\async\await(React\Promise\Timer\sleep(1.0));
React\Async\await(React\Promise\Timer\sleep(1.0));
echo 'c';
}));

Loop::addTimer(1.0, fn() => echo 'b');
Loop::addTimer(1.0, function () {
echo 'b';
});

// prints "a" at t=0.5s
// prints "b" at t=1.0s
Expand All @@ -98,13 +100,15 @@ In particular, this function does not "magically" make any blocking function
non-blocking:

```php
Loop::addTimer(0.5, React\Async\async(function() {
Loop::addTimer(0.5, React\Async\async(function () {
echo 'a';
sleep(1); // broken: using PHP's blocking sleep() for demonstration purposes
echo 'c';
}));

Loop::addTimer(1.0, fn() => echo 'b');
Loop::addTimer(1.0, function () {
echo 'b';
});

// prints "a" at t=0.5s
// prints "c" at t=1.5s: Correct timing, but wrong order
Expand Down Expand Up @@ -216,7 +220,7 @@ that bubbles up through the fibers.
```php
$promise = async(static function (): int {
echo 'a';
await(async(static function(): void {
await(async(static function (): void {
echo 'b';
await(React\Promise\Timer\sleep(2));
echo 'c';
Expand Down Expand Up @@ -247,13 +251,15 @@ call. Everything inside this function will still be blocked, but everything
outside this function can be executed asynchronously without blocking:

```php
Loop::addTimer(0.5, React\Async\async(function() {
Loop::addTimer(0.5, React\Async\async(function () {
echo 'a';
React\async\await(React\Promise\Timer\sleep(1.0));
React\Async\await(React\Promise\Timer\sleep(1.0));
echo 'c';
}));

Loop::addTimer(1.0, fn() => echo 'b');
Loop::addTimer(1.0, function () {
echo 'b';
});

// prints "a" at t=0.5s
// prints "b" at t=1.0s
Expand Down
24 changes: 15 additions & 9 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
* asynchronously without blocking:
*
* ```php
* Loop::addTimer(0.5, React\Async\async(function() {
* Loop::addTimer(0.5, React\Async\async(function () {
* echo 'a';
* React\async\await(React\Promise\Timer\sleep(1.0));
* React\Async\await(React\Promise\Timer\sleep(1.0));
* echo 'c';
* }));
*
* Loop::addTimer(1.0, fn() => echo 'b');
* Loop::addTimer(1.0, function () {
* echo 'b';
* });
*
* // prints "a" at t=0.5s
* // prints "b" at t=1.0s
Expand All @@ -39,13 +41,15 @@
* non-blocking:
*
* ```php
* Loop::addTimer(0.5, React\Async\async(function() {
* Loop::addTimer(0.5, React\Async\async(function () {
* echo 'a';
* sleep(1); // broken: using PHP's blocking sleep() for demonstration purposes
* echo 'c';
* }));
*
* Loop::addTimer(1.0, fn() => echo 'b');
* Loop::addTimer(1.0, function () {
* echo 'b';
* });
*
* // prints "a" at t=0.5s
* // prints "c" at t=1.5s: Correct timing, but wrong order
Expand Down Expand Up @@ -157,7 +161,7 @@
* ```php
* $promise = async(static function (): int {
* echo 'a';
* await(async(static function(): void {
* await(async(static function (): void {
* echo 'b';
* await(React\Promise\Timer\sleep(2));
* echo 'c';
Expand Down Expand Up @@ -227,13 +231,15 @@ function async(callable $function): callable
* outside this function can be executed asynchronously without blocking:
*
* ```php
* Loop::addTimer(0.5, React\Async\async(function() {
* Loop::addTimer(0.5, React\Async\async(function () {
* echo 'a';
* React\async\await(React\Promise\Timer\sleep(1.0));
* React\Async\await(React\Promise\Timer\sleep(1.0));
* echo 'c';
* }));
*
* Loop::addTimer(1.0, fn() => echo 'b');
* Loop::addTimer(1.0, function () {
* echo 'b';
* });
*
* // prints "a" at t=0.5s
* // prints "b" at t=1.0s
Expand Down
4 changes: 2 additions & 2 deletions tests/AsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ public function testCancelAsyncWillCancelNestedAwait()

$promise = async(static function (): int {
echo 'a';
await(async(static function(): void {
await(async(static function (): void {
echo 'b';
await(async(static function(): void {
await(async(static function (): void {
echo 'c';
await(new Promise(function () { }, function () {
throw new \RuntimeException('Operation cancelled');
Expand Down

0 comments on commit 4dae336

Please sign in to comment.