Skip to content

Commit

Permalink
Merge pull request #63 from clue-labs/docs
Browse files Browse the repository at this point in the history
Add documentation to describe limitations of blocking vs async APIs
  • Loading branch information
SimonFrings authored Oct 20, 2021
2 parents ee21147 + d9f50cf commit 7618d1b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ use the [default loop](https://github.com/reactphp/event-loop#loop). This value
SHOULD NOT be given unless you're sure you want to explicitly use a given event
loop instance.

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

### await()

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

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

### awaitAny()

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

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

### awaitAll()

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

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

## Install

The recommended way to install this library is [through Composer](https://getcomposer.org).
Expand Down
28 changes: 28 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
* SHOULD NOT be given unless you're sure you want to explicitly use a given event
* loop instance.
*
* Note that this function will assume control over the event loop. Internally, it
* will actually `run()` the loop until the timer fires and then calls `stop()` to
* terminate execution of the loop. This means this function is more suited for
* short-lived program executions when using async APIs is not feasible. For
* long-running applications, using event-driven APIs by leveraging timers
* is usually preferable.
*
* @param float $time
* @param ?LoopInterface $loop
* @return void
Expand Down Expand Up @@ -92,6 +99,13 @@ function sleep($time, LoopInterface $loop = null)
* This implies that if you pass a really small (or negative) value, it will still
* start a timer and will thus trigger at the earliest possible time in the future.
*
* Note that this function will assume control over the event loop. Internally, it
* will actually `run()` the loop until the promise settles and then calls `stop()` to
* terminate execution of the loop. This means this function is more suited for
* short-lived promise executions when using promise-based APIs is not feasible.
* For long-running applications, using promise-based APIs by leveraging chained
* `then()` calls is usually preferable.
*
* @param PromiseInterface $promise
* @param ?LoopInterface $loop
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
Expand Down Expand Up @@ -196,6 +210,13 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
* value, it will still start a timer and will thus trigger at the earliest
* possible time in the future.
*
* Note that this function will assume control over the event loop. Internally, it
* will actually `run()` the loop until the promise settles and then calls `stop()` to
* terminate execution of the loop. This means this function is more suited for
* short-lived promise executions when using promise-based APIs is not feasible.
* For long-running applications, using promise-based APIs by leveraging chained
* `then()` calls is usually preferable.
*
* @param PromiseInterface[] $promises
* @param ?LoopInterface $loop
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
Expand Down Expand Up @@ -287,6 +308,13 @@ function awaitAny(array $promises, LoopInterface $loop = null, $timeout = null)
* value, it will still start a timer and will thus trigger at the earliest
* possible time in the future.
*
* Note that this function will assume control over the event loop. Internally, it
* will actually `run()` the loop until the promise settles and then calls `stop()` to
* terminate execution of the loop. This means this function is more suited for
* short-lived promise executions when using promise-based APIs is not feasible.
* For long-running applications, using promise-based APIs by leveraging chained
* `then()` calls is usually preferable.
*
* @param PromiseInterface[] $promises
* @param ?LoopInterface $loop
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
Expand Down

0 comments on commit 7618d1b

Please sign in to comment.