Skip to content

Commit f5d8b97

Browse files
committed
Improve examples for async() and await()
1 parent 257634a commit f5d8b97

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ will still be blocked, but everything outside this function can be executed
7878
asynchronously without blocking:
7979

8080
```php
81-
Loop::addTimer(0.5, React\Async\async(function() {
81+
Loop::addTimer(0.5, React\Async\async(function () {
8282
echo 'a';
83-
React\async\await(React\Promise\Timer\sleep(1.0));
83+
React\Async\await(React\Promise\Timer\sleep(1.0));
8484
echo 'c';
8585
}));
8686

87-
Loop::addTimer(1.0, fn() => echo 'b');
87+
Loop::addTimer(1.0, function () {
88+
echo 'b';
89+
});
8890

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

100102
```php
101-
Loop::addTimer(0.5, React\Async\async(function() {
103+
Loop::addTimer(0.5, React\Async\async(function () {
102104
echo 'a';
103105
sleep(1); // broken: using PHP's blocking sleep() for demonstration purposes
104106
echo 'c';
105107
}));
106108

107-
Loop::addTimer(1.0, fn() => echo 'b');
109+
Loop::addTimer(1.0, function () {
110+
echo 'b';
111+
});
108112

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

249253
```php
250-
Loop::addTimer(0.5, React\Async\async(function() {
254+
Loop::addTimer(0.5, React\Async\async(function () {
251255
echo 'a';
252-
React\async\await(React\Promise\Timer\sleep(1.0));
256+
React\Async\await(React\Promise\Timer\sleep(1.0));
253257
echo 'c';
254258
}));
255259

256-
Loop::addTimer(1.0, fn() => echo 'b');
260+
Loop::addTimer(1.0, function () {
261+
echo 'b';
262+
});
257263

258264
// prints "a" at t=0.5s
259265
// prints "b" at t=1.0s

src/functions.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
* asynchronously without blocking:
2020
*
2121
* ```php
22-
* Loop::addTimer(0.5, React\Async\async(function() {
22+
* Loop::addTimer(0.5, React\Async\async(function () {
2323
* echo 'a';
24-
* React\async\await(React\Promise\Timer\sleep(1.0));
24+
* React\Async\await(React\Promise\Timer\sleep(1.0));
2525
* echo 'c';
2626
* }));
2727
*
28-
* Loop::addTimer(1.0, fn() => echo 'b');
28+
* Loop::addTimer(1.0, function () {
29+
* echo 'b';
30+
* });
2931
*
3032
* // prints "a" at t=0.5s
3133
* // prints "b" at t=1.0s
@@ -39,13 +41,15 @@
3941
* non-blocking:
4042
*
4143
* ```php
42-
* Loop::addTimer(0.5, React\Async\async(function() {
44+
* Loop::addTimer(0.5, React\Async\async(function () {
4345
* echo 'a';
4446
* sleep(1); // broken: using PHP's blocking sleep() for demonstration purposes
4547
* echo 'c';
4648
* }));
4749
*
48-
* Loop::addTimer(1.0, fn() => echo 'b');
50+
* Loop::addTimer(1.0, function () {
51+
* echo 'b';
52+
* });
4953
*
5054
* // prints "a" at t=0.5s
5155
* // prints "c" at t=1.5s: Correct timing, but wrong order
@@ -157,7 +161,7 @@
157161
* ```php
158162
* $promise = async(static function (): int {
159163
* echo 'a';
160-
* await(async(static function(): void {
164+
* await(async(static function (): void {
161165
* echo 'b';
162166
* await(React\Promise\Timer\sleep(2));
163167
* echo 'c';
@@ -227,13 +231,15 @@ function async(callable $function): callable
227231
* outside this function can be executed asynchronously without blocking:
228232
*
229233
* ```php
230-
* Loop::addTimer(0.5, React\Async\async(function() {
234+
* Loop::addTimer(0.5, React\Async\async(function () {
231235
* echo 'a';
232-
* React\async\await(React\Promise\Timer\sleep(1.0));
236+
* React\Async\await(React\Promise\Timer\sleep(1.0));
233237
* echo 'c';
234238
* }));
235239
*
236-
* Loop::addTimer(1.0, fn() => echo 'b');
240+
* Loop::addTimer(1.0, function () {
241+
* echo 'b';
242+
* });
237243
*
238244
* // prints "a" at t=0.5s
239245
* // prints "b" at t=1.0s

tests/AsyncTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ public function testCancelAsyncWillCancelNestedAwait()
243243

244244
$promise = async(static function (): int {
245245
echo 'a';
246-
await(async(static function(): void {
246+
await(async(static function (): void {
247247
echo 'b';
248-
await(async(static function(): void {
248+
await(async(static function (): void {
249249
echo 'c';
250250
await(new Promise(function () { }, function () {
251251
throw new \RuntimeException('Operation cancelled');

0 commit comments

Comments
 (0)