Skip to content

Commit 7618d1b

Browse files
authored
Merge pull request #63 from clue-labs/docs
Add documentation to describe limitations of blocking vs async APIs
2 parents ee21147 + d9f50cf commit 7618d1b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ use the [default loop](https://github.com/reactphp/event-loop#loop). This value
120120
SHOULD NOT be given unless you're sure you want to explicitly use a given event
121121
loop instance.
122122

123+
Note that this function will assume control over the event loop. Internally, it
124+
will actually `run()` the loop until the timer fires and then calls `stop()` to
125+
terminate execution of the loop. This means this function is more suited for
126+
short-lived program executions when using async APIs is not feasible. For
127+
long-running applications, using event-driven APIs by leveraging timers
128+
is usually preferable.
129+
123130
### await()
124131

125132
The `await(PromiseInterface $promise, ?LoopInterface $loop = null, ?float $timeout = null): mixed` function can be used to
@@ -170,6 +177,13 @@ timeout triggers, this will `cancel()` the promise and throw a `TimeoutException
170177
This implies that if you pass a really small (or negative) value, it will still
171178
start a timer and will thus trigger at the earliest possible time in the future.
172179

180+
Note that this function will assume control over the event loop. Internally, it
181+
will actually `run()` the loop until the promise settles and then calls `stop()` to
182+
terminate execution of the loop. This means this function is more suited for
183+
short-lived promise executions when using promise-based APIs is not feasible.
184+
For long-running applications, using promise-based APIs by leveraging chained
185+
`then()` calls is usually preferable.
186+
173187
### awaitAny()
174188

175189
The `awaitAny(PromiseInterface[] $promises, ?LoopInterface $loop = null, ?float $timeout = null): mixed` function can be used to
@@ -216,6 +230,13 @@ the timeout triggers, this will `cancel()` all pending promises and throw a
216230
value, it will still start a timer and will thus trigger at the earliest
217231
possible time in the future.
218232

233+
Note that this function will assume control over the event loop. Internally, it
234+
will actually `run()` the loop until the promise settles and then calls `stop()` to
235+
terminate execution of the loop. This means this function is more suited for
236+
short-lived promise executions when using promise-based APIs is not feasible.
237+
For long-running applications, using promise-based APIs by leveraging chained
238+
`then()` calls is usually preferable.
239+
219240
### awaitAll()
220241

221242
The `awaitAll(PromiseInterface[] $promises, ?LoopInterface $loop = null, ?float $timeout = null): mixed[]` function can be used to
@@ -265,6 +286,13 @@ the timeout triggers, this will `cancel()` all pending promises and throw a
265286
value, it will still start a timer and will thus trigger at the earliest
266287
possible time in the future.
267288

289+
Note that this function will assume control over the event loop. Internally, it
290+
will actually `run()` the loop until the promise settles and then calls `stop()` to
291+
terminate execution of the loop. This means this function is more suited for
292+
short-lived promise executions when using promise-based APIs is not feasible.
293+
For long-running applications, using promise-based APIs by leveraging chained
294+
`then()` calls is usually preferable.
295+
268296
## Install
269297

270298
The recommended way to install this library is [through Composer](https://getcomposer.org).

src/functions.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
* SHOULD NOT be given unless you're sure you want to explicitly use a given event
3636
* loop instance.
3737
*
38+
* Note that this function will assume control over the event loop. Internally, it
39+
* will actually `run()` the loop until the timer fires and then calls `stop()` to
40+
* terminate execution of the loop. This means this function is more suited for
41+
* short-lived program executions when using async APIs is not feasible. For
42+
* long-running applications, using event-driven APIs by leveraging timers
43+
* is usually preferable.
44+
*
3845
* @param float $time
3946
* @param ?LoopInterface $loop
4047
* @return void
@@ -92,6 +99,13 @@ function sleep($time, LoopInterface $loop = null)
9299
* This implies that if you pass a really small (or negative) value, it will still
93100
* start a timer and will thus trigger at the earliest possible time in the future.
94101
*
102+
* Note that this function will assume control over the event loop. Internally, it
103+
* will actually `run()` the loop until the promise settles and then calls `stop()` to
104+
* terminate execution of the loop. This means this function is more suited for
105+
* short-lived promise executions when using promise-based APIs is not feasible.
106+
* For long-running applications, using promise-based APIs by leveraging chained
107+
* `then()` calls is usually preferable.
108+
*
95109
* @param PromiseInterface $promise
96110
* @param ?LoopInterface $loop
97111
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
@@ -196,6 +210,13 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
196210
* value, it will still start a timer and will thus trigger at the earliest
197211
* possible time in the future.
198212
*
213+
* Note that this function will assume control over the event loop. Internally, it
214+
* will actually `run()` the loop until the promise settles and then calls `stop()` to
215+
* terminate execution of the loop. This means this function is more suited for
216+
* short-lived promise executions when using promise-based APIs is not feasible.
217+
* For long-running applications, using promise-based APIs by leveraging chained
218+
* `then()` calls is usually preferable.
219+
*
199220
* @param PromiseInterface[] $promises
200221
* @param ?LoopInterface $loop
201222
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
@@ -287,6 +308,13 @@ function awaitAny(array $promises, LoopInterface $loop = null, $timeout = null)
287308
* value, it will still start a timer and will thus trigger at the earliest
288309
* possible time in the future.
289310
*
311+
* Note that this function will assume control over the event loop. Internally, it
312+
* will actually `run()` the loop until the promise settles and then calls `stop()` to
313+
* terminate execution of the loop. This means this function is more suited for
314+
* short-lived promise executions when using promise-based APIs is not feasible.
315+
* For long-running applications, using promise-based APIs by leveraging chained
316+
* `then()` calls is usually preferable.
317+
*
290318
* @param PromiseInterface[] $promises
291319
* @param ?LoopInterface $loop
292320
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever

0 commit comments

Comments
 (0)