|
2 | 2 |
|
3 | 3 | namespace React\Async; |
4 | 4 |
|
| 5 | +use React\EventLoop\Loop; |
| 6 | +use React\EventLoop\LoopInterface; |
5 | 7 | use React\Promise\Deferred; |
6 | 8 | use React\Promise\PromiseInterface; |
| 9 | +use React\Promise\Timer; |
| 10 | + |
| 11 | +/** |
| 12 | + * Block waiting for the given `$promise` to be fulfilled. |
| 13 | + * |
| 14 | + * ```php |
| 15 | + * $result = React\Async\await($promise, $loop); |
| 16 | + * ``` |
| 17 | + * |
| 18 | + * This function will only return after the given `$promise` has settled, i.e. |
| 19 | + * either fulfilled or rejected. In the meantime, the event loop will run any |
| 20 | + * events attached to the same loop until the promise settles. |
| 21 | + * |
| 22 | + * Once the promise is fulfilled, this function will return whatever the promise |
| 23 | + * resolved to. |
| 24 | + * |
| 25 | + * Once the promise is rejected, this will throw whatever the promise rejected |
| 26 | + * with. If the promise did not reject with an `Exception`, then this function |
| 27 | + * will throw an `UnexpectedValueException` instead. |
| 28 | + * |
| 29 | + * ```php |
| 30 | + * try { |
| 31 | + * $result = React\Async\await($promise, $loop); |
| 32 | + * // promise successfully fulfilled with $result |
| 33 | + * echo 'Result: ' . $result; |
| 34 | + * } catch (Exception $exception) { |
| 35 | + * // promise rejected with $exception |
| 36 | + * echo 'ERROR: ' . $exception->getMessage(); |
| 37 | + * } |
| 38 | + * ``` |
| 39 | + * |
| 40 | + * This function takes an optional `LoopInterface|null $loop` parameter that can be used to |
| 41 | + * pass the event loop instance to use. You can use a `null` value here in order to |
| 42 | + * use the [default loop](https://github.com/reactphp/event-loop#loop). This value |
| 43 | + * SHOULD NOT be given unless you're sure you want to explicitly use a given event |
| 44 | + * loop instance. |
| 45 | + * |
| 46 | + * If no `$timeout` argument is given and the promise stays pending, then this |
| 47 | + * will potentially wait/block forever until the promise is settled. To avoid |
| 48 | + * this, API authors creating promises are expected to provide means to |
| 49 | + * configure a timeout for the promise instead. For more details, see also the |
| 50 | + * [`timeout()` function](https://github.com/reactphp/promise-timer#timeout). |
| 51 | + * |
| 52 | + * If the deprecated `$timeout` argument is given and the promise is still pending once the |
| 53 | + * timeout triggers, this will `cancel()` the promise and throw a `TimeoutException`. |
| 54 | + * This implies that if you pass a really small (or negative) value, it will still |
| 55 | + * start a timer and will thus trigger at the earliest possible time in the future. |
| 56 | + * |
| 57 | + * Note that this function will assume control over the event loop. Internally, it |
| 58 | + * will actually `run()` the loop until the promise settles and then calls `stop()` to |
| 59 | + * terminate execution of the loop. This means this function is more suited for |
| 60 | + * short-lived promise executions when using promise-based APIs is not feasible. |
| 61 | + * For long-running applications, using promise-based APIs by leveraging chained |
| 62 | + * `then()` calls is usually preferable. |
| 63 | + * |
| 64 | + * @param PromiseInterface $promise |
| 65 | + * @param ?LoopInterface $loop |
| 66 | + * @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever |
| 67 | + * @return mixed returns whatever the promise resolves to |
| 68 | + * @throws \Exception when the promise is rejected |
| 69 | + * @throws \React\Promise\Timer\TimeoutException if the $timeout is given and triggers |
| 70 | + */ |
| 71 | +function await(PromiseInterface $promise, LoopInterface $loop = null, $timeout = null) |
| 72 | +{ |
| 73 | + $wait = true; |
| 74 | + $resolved = null; |
| 75 | + $exception = null; |
| 76 | + $rejected = false; |
| 77 | + $loop = $loop ?: Loop::get(); |
| 78 | + |
| 79 | + if ($timeout !== null) { |
| 80 | + $promise = Timer\timeout($promise, $timeout, $loop); |
| 81 | + } |
| 82 | + |
| 83 | + $promise->then( |
| 84 | + function ($c) use (&$resolved, &$wait, $loop) { |
| 85 | + $resolved = $c; |
| 86 | + $wait = false; |
| 87 | + $loop->stop(); |
| 88 | + }, |
| 89 | + function ($error) use (&$exception, &$rejected, &$wait, $loop) { |
| 90 | + $exception = $error; |
| 91 | + $rejected = true; |
| 92 | + $wait = false; |
| 93 | + $loop->stop(); |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + // Explicitly overwrite argument with null value. This ensure that this |
| 98 | + // argument does not show up in the stack trace in PHP 7+ only. |
| 99 | + $promise = null; |
| 100 | + |
| 101 | + while ($wait) { |
| 102 | + $loop->run(); |
| 103 | + } |
| 104 | + |
| 105 | + if ($rejected) { |
| 106 | + if (!$exception instanceof \Exception && !$exception instanceof \Throwable) { |
| 107 | + $exception = new \UnexpectedValueException( |
| 108 | + 'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception)) |
| 109 | + ); |
| 110 | + } elseif (!$exception instanceof \Exception) { |
| 111 | + $exception = new \UnexpectedValueException( |
| 112 | + 'Promise rejected with unexpected ' . get_class($exception) . ': ' . $exception->getMessage(), |
| 113 | + $exception->getCode(), |
| 114 | + $exception |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + throw $exception; |
| 119 | + } |
| 120 | + |
| 121 | + return $resolved; |
| 122 | +} |
7 | 123 |
|
8 | 124 | /** |
9 | 125 | * @param array<callable():PromiseInterface<mixed,Exception>> $tasks |
|
0 commit comments