@@ -88,119 +88,134 @@ $loop = React\EventLoop\Factory::create();
88
88
89
89
### sleep()
90
90
91
- The ` sleep($seconds, LoopInterface $loop) ` method can be used to wait/sleep for $time seconds.
91
+ The ` sleep($seconds, LoopInterface $loop): void ` function can be used to
92
+ wait/sleep for ` $time ` seconds.
92
93
93
94
``` php
94
95
Block\sleep(1.5, $loop);
95
96
```
96
97
97
- The $time value will be used as a timer for the loop so that it keeps running
98
- until the timeout triggers.
99
- This implies that if you pass a really small (or negative) value, it will still
100
- start a timer and will thus trigger at the earliest possible time in the future .
98
+ This function will only return after the given ` $time ` has elapsed. In the
99
+ meantime, the event loop will run any other events attached to the same loop
100
+ until the timer fires. If there are no other events attached to this loop,
101
+ it will behave similar to the built- in [ ` sleep() ` ] ( https://www.php.net/manual/en/function.sleep.php ) .
101
102
102
- While this may look similar to PHP's [ ` sleep() ` ] ( https://www.php.net/sleep ) function,
103
- it's actual way more powerful:
104
- Instead of making the whole process sleep and handing over control to your operating system,
105
- this function actually executes the loop in the meantime.
106
- This is particularly useful if you've attached more async tasks to the same loop instance.
107
- If there are no other (async) tasks, this will behave similar to ` sleep() ` .
103
+ Internally, the ` $time ` argument will be used as a timer for the loop so that
104
+ it keeps running until this timer triggers. This implies that if you pass a
105
+ really small (or negative) value, it will still start a timer and will thus
106
+ trigger at the earliest possible time in the future.
108
107
109
108
### await()
110
109
111
- The ` await(PromiseInterface $promise, LoopInterface $loop, $timeout = null) `
112
- function can be used to block waiting for the given $promise to resolve .
110
+ The ` await(PromiseInterface $promise, LoopInterface $loop, ?float $timeout = null): mixed ` function can be used to
111
+ Block waiting for the given ` $promise ` to be fulfilled .
113
112
114
113
``` php
115
- $result = Block\await($promise, $loop);
114
+ $result = Block\await($promise, $loop, $timeout );
116
115
```
117
116
118
- Once the promise is resolved, this will return whatever the promise resolves to.
117
+ This function will only return after the given ` $promise ` has settled, i.e.
118
+ either fulfilled or rejected. In the meantime, the event loop will run any
119
+ events attached to the same loop until the promise settles.
120
+
121
+ Once the promise is fulfilled, this function will return whatever the promise
122
+ resolved to.
119
123
120
- Once the promise is rejected, this will throw whatever the promise rejected with.
121
- If the promise did not reject with an ` Exception ` , then this function will
122
- throw an ` UnexpectedValueException ` instead.
124
+ Once the promise is rejected, this will throw whatever the promise rejected
125
+ with. If the promise did not reject with an ` Exception ` , then this function
126
+ will throw an ` UnexpectedValueException ` instead.
123
127
124
128
``` php
125
129
try {
126
- $value = Block\await($promise, $loop);
130
+ $result = Block\await($promise, $loop);
127
131
// promise successfully fulfilled with $value
128
- echo 'Result: ' . $value ;
132
+ echo 'Result: ' . $result ;
129
133
} catch (Exception $exception) {
130
134
// promise rejected with $exception
131
135
echo 'ERROR: ' . $exception->getMessage();
132
136
}
133
137
```
134
138
135
- If no $timeout is given and the promise stays pending, then this will
136
- potentially wait/block forever until the promise is settled.
139
+ If no ` $timeout ` argument is given and the promise stays pending, then this
140
+ will potentially wait/block forever until the promise is settled.
137
141
138
- If a $timeout is given and the promise is still pending once the timeout
139
- triggers, this will ` cancel() ` the promise and throw a ` TimeoutException ` .
142
+ If a ` $timeout ` argument is given and the promise is still pending once the
143
+ timeout triggers, this will ` cancel() ` the promise and throw a ` TimeoutException ` .
140
144
This implies that if you pass a really small (or negative) value, it will still
141
145
start a timer and will thus trigger at the earliest possible time in the future.
142
146
143
147
### awaitAny()
144
148
145
- The ` awaitAny(array $promises, LoopInterface $loop, $timeout = null) `
146
- function can be used to wait for ANY of the given promises to resolve .
149
+ The ` awaitAny(PromiseInterface[] $promises, LoopInterface $loop, ?float $timeout = null): mixed ` function can be used to
150
+ wait for ANY of the given promises to be fulfilled .
147
151
148
152
``` php
149
153
$promises = array(
150
154
$promise1,
151
155
$promise2
152
156
);
153
157
154
- $firstResult = Block\awaitAny($promises, $loop);
158
+ $firstResult = Block\awaitAny($promises, $loop, $timeout );
155
159
156
160
echo 'First result: ' . $firstResult;
157
161
```
158
162
159
- Once the first promise is resolved, this will try to ` cancel() ` all
160
- remaining promises and return whatever the first promise resolves to.
163
+ This function will only return after ANY of the given ` $promises ` has been
164
+ fulfilled or will throw when ALL of them have been rejected. In the meantime,
165
+ the event loop will run any events attached to the same loop.
161
166
162
- If ALL promises fail to resolve, this will fail and throw an ` Exception ` .
167
+ Once ANY promise is fulfilled, this function will return whatever this
168
+ promise resolved to and will try to ` cancel() ` all remaining promises.
163
169
164
- If no $timeout is given and either promise stays pending, then this will
165
- potentially wait/block forever until the first promise is settled .
170
+ Once ALL promises reject, this function will fail and throw an ` UnderflowException ` .
171
+ Likewise, this will throw if an empty array of ` $promises ` is passed .
166
172
167
- If a $timeout is given and either promise is still pending once the timeout
168
- triggers, this will ` cancel() ` all pending promises and throw a ` TimeoutException ` .
169
- This implies that if you pass a really small (or negative) value, it will still
170
- start a timer and will thus trigger at the earliest possible time in the future.
173
+ If no ` $timeout ` argument is given and ALL promises stay pending, then this
174
+ will potentially wait/block forever until the promise is fulfilled.
175
+
176
+ If a ` $timeout ` argument is given and ANY promises are still pending once
177
+ the timeout triggers, this will ` cancel() ` all pending promises and throw a
178
+ ` TimeoutException ` . This implies that if you pass a really small (or negative)
179
+ value, it will still start a timer and will thus trigger at the earliest
180
+ possible time in the future.
171
181
172
182
### awaitAll()
173
183
174
- The ` awaitAll(array $promises, LoopInterface $loop, $timeout = null) `
175
- function can be used to wait for ALL of the given promises to resolve .
184
+ The ` awaitAll(PromiseInterface[] $promises, LoopInterface $loop, ?float $timeout = null): mixed[] ` function can be used to
185
+ wait for ALL of the given promises to be fulfilled .
176
186
177
187
``` php
178
188
$promises = array(
179
189
$promise1,
180
190
$promise2
181
191
);
182
192
183
- $allResults = Block\awaitAll($promises, $loop);
193
+ $allResults = Block\awaitAll($promises, $loop, $timeout );
184
194
185
195
echo 'First promise resolved with: ' . $allResults[0];
186
196
```
187
197
188
- Once the last promise resolves, this will return an array with whatever
198
+ This function will only return after ALL of the given ` $promises ` have been
199
+ fulfilled or will throw when ANY of them have been rejected. In the meantime,
200
+ the event loop will run any events attached to the same loop.
201
+
202
+ Once ALL promises are fulfilled, this will return an array with whatever
189
203
each promise resolves to. Array keys will be left intact, i.e. they can
190
204
be used to correlate the return array to the promises passed.
205
+ Likewise, this will return an empty array if an empty array of ` $promises ` is passed.
191
206
192
- If ANY promise fails to resolve, this will try to ` cancel() ` all
193
- remaining promises and throw an ` Exception ` .
194
- If the promise did not reject with an ` Exception ` , then this function will
195
- throw an ` UnexpectedValueException ` instead.
207
+ Once ANY promise rejects, this will try to ` cancel() ` all remaining promises
208
+ and throw an ` Exception ` . If the promise did not reject with an ` Exception ` ,
209
+ then this function will throw an ` UnexpectedValueException ` instead.
196
210
197
- If no $timeout is given and either promise stays pending, then this will
198
- potentially wait/block forever until the last promise is settled .
211
+ If no ` $timeout ` argument is given and ANY promises stay pending, then this
212
+ will potentially wait/block forever until the promise is fulfilled .
199
213
200
- If a $timeout is given and either promise is still pending once the timeout
201
- triggers, this will ` cancel() ` all pending promises and throw a ` TimeoutException ` .
202
- This implies that if you pass a really small (or negative) value, it will still
203
- start a timer and will thus trigger at the earliest possible time in the future.
214
+ If a ` $timeout ` argument is given and ANY promises are still pending once
215
+ the timeout triggers, this will ` cancel() ` all pending promises and throw a
216
+ ` TimeoutException ` . This implies that if you pass a really small (or negative)
217
+ value, it will still start a timer and will thus trigger at the earliest
218
+ possible time in the future.
204
219
205
220
## Install
206
221
0 commit comments