@@ -193,11 +193,11 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)
193193}
194194
195195/**
196- * Create a new promise that resolves in `$time` seconds with the `$time` as the fulfillment value .
196+ * Create a new promise that resolves in `$time` seconds.
197197 *
198198 * ```php
199- * React\Promise\Timer\resolve (1.5)->then(function ($time ) {
200- * echo 'Thanks for waiting ' . $time . ' seconds ' . PHP_EOL;
199+ * React\Promise\Timer\sleep (1.5)->then(function () {
200+ * echo 'Thanks for waiting! ' . PHP_EOL;
201201 * });
202202 * ```
203203 *
@@ -217,16 +217,16 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)
217217 * with a `RuntimeException` and clean up any pending timers.
218218 *
219219 * ```php
220- * $timer = React\Promise\Timer\resolve (2.0);
220+ * $timer = React\Promise\Timer\sleep (2.0);
221221 *
222222 * $timer->cancel();
223223 * ```
224224 *
225225 * @param float $time
226226 * @param ?LoopInterface $loop
227- * @return PromiseInterface<float , \RuntimeException>
227+ * @return PromiseInterface<void , \RuntimeException>
228228 */
229- function resolve ($ time , LoopInterface $ loop = null )
229+ function sleep ($ time , LoopInterface $ loop = null )
230230{
231231 if ($ loop === null ) {
232232 $ loop = Loop::get ();
@@ -235,8 +235,8 @@ function resolve($time, LoopInterface $loop = null)
235235 $ timer = null ;
236236 return new Promise (function ($ resolve ) use ($ loop , $ time , &$ timer ) {
237237 // resolve the promise when the timer fires in $time seconds
238- $ timer = $ loop ->addTimer ($ time , function () use ($ time , $ resolve ) {
239- $ resolve ($ time );
238+ $ timer = $ loop ->addTimer ($ time , function () use ($ resolve ) {
239+ $ resolve ();
240240 });
241241 }, function () use (&$ timer , $ loop ) {
242242 // cancelling this promise will cancel the timer, clean the reference
@@ -249,7 +249,50 @@ function resolve($time, LoopInterface $loop = null)
249249}
250250
251251/**
252- * Create a new promise which rejects in `$time` seconds with a `TimeoutException`.
252+ * [Deprecated] Create a new promise that resolves in `$time` seconds with the `$time` as the fulfillment value.
253+ *
254+ * ```php
255+ * React\Promise\Timer\resolve(1.5)->then(function ($time) {
256+ * echo 'Thanks for waiting ' . $time . ' seconds' . PHP_EOL;
257+ * });
258+ * ```
259+ *
260+ * Internally, the given `$time` value will be used to start a timer that will
261+ * resolve the promise once it triggers. This implies that if you pass a really
262+ * small (or negative) value, it will still start a timer and will thus trigger
263+ * at the earliest possible time in the future.
264+ *
265+ * This function takes an optional `LoopInterface|null $loop` parameter that can be used to
266+ * pass the event loop instance to use. You can use a `null` value here in order to
267+ * use the [default loop](https://github.com/reactphp/event-loop#loop). This value
268+ * SHOULD NOT be given unless you're sure you want to explicitly use a given event
269+ * loop instance.
270+ *
271+ * The returned promise is implemented in such a way that it can be cancelled
272+ * when it is still pending. Cancelling a pending promise will reject its value
273+ * with a `RuntimeException` and clean up any pending timers.
274+ *
275+ * ```php
276+ * $timer = React\Promise\Timer\resolve(2.0);
277+ *
278+ * $timer->cancel();
279+ * ```
280+ *
281+ * @param float $time
282+ * @param ?LoopInterface $loop
283+ * @return PromiseInterface<float, \RuntimeException>
284+ * @deprecated 1.8.0 See `sleep()` instead
285+ * @see sleep()
286+ */
287+ function resolve ($ time , LoopInterface $ loop = null )
288+ {
289+ return sleep ($ time , $ loop )->then (function () use ($ time ) {
290+ return $ time ;
291+ });
292+ }
293+
294+ /**
295+ * [Deprecated] Create a new promise which rejects in `$time` seconds with a `TimeoutException`.
253296 *
254297 * ```php
255298 * React\Promise\Timer\reject(2.0)->then(null, function (React\Promise\Timer\TimeoutException $e) {
@@ -281,10 +324,12 @@ function resolve($time, LoopInterface $loop = null)
281324 * @param float $time
282325 * @param LoopInterface $loop
283326 * @return PromiseInterface<void, TimeoutException|\RuntimeException>
327+ * @deprecated 1.8.0 See `sleep()` instead
328+ * @see sleep()
284329 */
285330function reject ($ time , LoopInterface $ loop = null )
286331{
287- return resolve ($ time , $ loop )->then (function ($ time ) {
332+ return sleep ($ time , $ loop )->then (function () use ($ time ) {
288333 throw new TimeoutException ($ time , 'Timer expired after ' . $ time . ' seconds ' );
289334 });
290335}
0 commit comments