Skip to content

Improve examples for async() and await() #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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