@@ -193,11 +193,11 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)
193
193
}
194
194
195
195
/**
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.
197
197
*
198
198
* ```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;
201
201
* });
202
202
* ```
203
203
*
@@ -217,16 +217,16 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)
217
217
* with a `RuntimeException` and clean up any pending timers.
218
218
*
219
219
* ```php
220
- * $timer = React\Promise\Timer\resolve (2.0);
220
+ * $timer = React\Promise\Timer\sleep (2.0);
221
221
*
222
222
* $timer->cancel();
223
223
* ```
224
224
*
225
225
* @param float $time
226
226
* @param ?LoopInterface $loop
227
- * @return PromiseInterface<float , \RuntimeException>
227
+ * @return PromiseInterface<void , \RuntimeException>
228
228
*/
229
- function resolve ($ time , LoopInterface $ loop = null )
229
+ function sleep ($ time , LoopInterface $ loop = null )
230
230
{
231
231
if ($ loop === null ) {
232
232
$ loop = Loop::get ();
@@ -235,8 +235,8 @@ function resolve($time, LoopInterface $loop = null)
235
235
$ timer = null ;
236
236
return new Promise (function ($ resolve ) use ($ loop , $ time , &$ timer ) {
237
237
// 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 ();
240
240
});
241
241
}, function () use (&$ timer , $ loop ) {
242
242
// cancelling this promise will cancel the timer, clean the reference
@@ -249,7 +249,50 @@ function resolve($time, LoopInterface $loop = null)
249
249
}
250
250
251
251
/**
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`.
253
296
*
254
297
* ```php
255
298
* React\Promise\Timer\reject(2.0)->then(null, function (React\Promise\Timer\TimeoutException $e) {
@@ -281,10 +324,12 @@ function resolve($time, LoopInterface $loop = null)
281
324
* @param float $time
282
325
* @param LoopInterface $loop
283
326
* @return PromiseInterface<void, TimeoutException|\RuntimeException>
327
+ * @deprecated 1.8.0 See `sleep()` instead
328
+ * @see sleep()
284
329
*/
285
330
function reject ($ time , LoopInterface $ loop = null )
286
331
{
287
- return resolve ($ time , $ loop )->then (function ($ time ) {
332
+ return sleep ($ time , $ loop )->then (function () use ($ time ) {
288
333
throw new TimeoutException ($ time , 'Timer expired after ' . $ time . ' seconds ' );
289
334
});
290
335
}
0 commit comments