@@ -132,16 +132,91 @@ If no cancellation handler is passed to the `Promise` constructor, then invoking
132132its ` cancel() ` method it is effectively a NO-OP.
133133This means that it may still be pending and can hence continue consuming resources.
134134
135- > Note: If you're stuck on legacy versions (PHP 5.3), then this is also a NO-OP,
136- as the Promise cancellation API is currently only available in
137- [ react/promise v2.1.0] ( https://github.com/reactphp/promise )
138- which in turn requires PHP 5.4 or up.
139- It is assumed that if you're actually still stuck on PHP 5.3, resource cleanup
140- is likely one of your smaller problems.
141-
142135For more details on the promise cancellation, please refer to the
143136[ Promise documentation] ( https://github.com/reactphp/promise#cancellablepromiseinterface ) .
144137
138+ #### Input cancellation
139+
140+ Irrespective of the timout handling, you can also explicitly ` cancel() ` the
141+ input ` $promise ` at any time.
142+ This means that the ` timeout() ` handling does not affect cancellation of the
143+ input ` $promise ` , as demonstrated in the following example:
144+
145+ ``` php
146+ $promise = accessSomeRemoteResource();
147+ $timeout = Timer\timeout($promise, 10.0, $loop);
148+
149+ $promise->cancel();
150+ ```
151+
152+ The registered [ cancellation handler] ( #cancellation-handler ) is responsible for
153+ handling the ` cancel() ` call:
154+
155+ * A described above, a common use involves resource cleanup and will then * reject*
156+ the ` Promise ` .
157+ If the input ` $promise ` is being rejected, then the timeout will be aborted
158+ and the resulting promise will also be rejected.
159+ * If the input ` $promise ` is still pending, then the timout will continue
160+ running until the timer expires.
161+ The same happens if the input ` $promise ` does not register a
162+ [ cancellation handler] ( #cancellation-handler ) .
163+
164+ #### Output cancellation
165+
166+ Similarily, you can also explicitly ` cancel() ` the resulting promise like this:
167+
168+ ``` php
169+ $promise = accessSomeRemoteResource();
170+ $timeout = Timer\timeout($promise, 10.0, $loop);
171+
172+ $timeout->cancel();
173+ ```
174+
175+ Note how this looks very similar to the above [ input cancellation] ( #input-cancellation )
176+ example. Accordingly, it also behaves very similar.
177+
178+ Calling ` cancel() ` on the resulting promise will merely try
179+ to ` cancel() ` the input ` $promise ` .
180+ This means that we do not take over responsibility of the outcome and it's
181+ entirely up to the input ` $promise ` to handle cancellation support.
182+
183+ The registered [ cancellation handler] ( #cancellation-handler ) is responsible for
184+ handling the ` cancel() ` call:
185+
186+ * As described above, a common use involves resource cleanup and will then * reject*
187+ the ` Promise ` .
188+ If the input ` $promise ` is being rejected, then the timeout will be aborted
189+ and the resulting promise will also be rejected.
190+ * If the input ` $promise ` is still pending, then the timout will continue
191+ running until the timer expires.
192+ The same happens if the input ` $promise ` does not register a
193+ [ cancellation handler] ( #cancellation-handler ) .
194+
195+ To re-iterate, note that calling ` cancel() ` on the resulting promise will merely
196+ try to cancel the input ` $promise ` only.
197+ It is then up to the cancellation handler of the input promise to settle the promise.
198+ If the input promise is still pending when the timeout occurs, then the normal
199+ [ timeout cancellation] ( #timeout-cancellation ) handling will trigger, effectively rejecting
200+ the output promise with a [ ` TimeoutException ` ] ( #timeoutexception ) .
201+
202+ This is done for consistency with the [ timeout cancellation] ( #timeout-cancellation )
203+ handling and also because it is assumed this is often used like this:
204+
205+ ``` php
206+ $timeout = Timer\timeout(accessSomeRemoteResource(), 10.0, $loop);
207+
208+ $timeout->cancel();
209+ ```
210+
211+ As described above, this example works as expected and cleans up any resources
212+ allocated for the input ` $promise ` .
213+
214+ Note that if the given input ` $promise ` does not support cancellation, then this
215+ is a NO-OP.
216+ This means that while the resulting promise will still be rejected after the
217+ timeout, the underlying input ` $promise ` may still be pending and can hence
218+ continue consuming resources.
219+
145220#### Collections
146221
147222If you want to wait for multiple promises to resolve, you can use the normal promise primitives like this:
@@ -176,20 +251,44 @@ Timer\resolve(1.5, $loop)->then(function ($time) {
176251});
177252```
178253
254+ #### Resolve cancellation
255+
256+ You can explicitly ` cancel() ` the resulting timer promise at any time:
257+
258+ ``` php
259+ $timer = Timer\resolve(2.0, $loop);
260+
261+ $timer->cancel();
262+ ```
263+
264+ This will abort the timer and * reject* with a ` RuntimeException ` .
265+
179266### reject()
180267
181268The ` reject($time, LoopInterface $loop) ` function can be used to create a new Promise
182269which rejects in ` $time ` seconds with a ` TimeoutException ` .
183270
184271``` php
185272Timer\reject(2.0, $loop)->then(null, function (TimeoutException $e) {
186- echo '
273+ echo 'Rejected after ' . $e->getTimeout() . ' seconds ' . PHP_EOL;
187274});
188275```
189276
190277This function complements the [ ` resolve() ` ] ( #resolve ) function
191278and can be used as a basic building block for higher-level promise consumers.
192279
280+ #### Reject cancellation
281+
282+ You can explicitly ` cancel() ` the resulting timer promise at any time:
283+
284+ ``` php
285+ $timer = Timer\reject(2.0, $loop);
286+
287+ $timer->cancel();
288+ ```
289+
290+ This will abort the timer and * reject* with a ` RuntimeException ` .
291+
193292### TimeoutException
194293
195294The ` TimeoutException ` extends PHP's built-in ` RuntimeException ` .
@@ -209,12 +308,6 @@ The recommended way to install this library is [through composer](http://getcomp
209308}
210309```
211310
212- > Note: If you're stuck on legacy versions (PHP 5.3), then the ` cancel() ` method
213- is not available,
214- as the Promise cancellation API is currently only available in
215- [ react/promise v2.1.0] ( https://github.com/reactphp/promise )
216- which in turn requires PHP 5.4 or up.
217-
218311## License
219312
220313MIT
0 commit comments