@@ -173,6 +173,67 @@ is not available, as the Promise cancellation API is currently only available in
173173[ react/promise v2.1.0] ( https://github.com/reactphp/promise )
174174which in turn requires PHP 5.4 or up.
175175
176+ #### Output cancellation
177+
178+ Similarily, you can also explicitly ` cancel() ` the resulting promise like this:
179+
180+ ``` php
181+ $promise = accessSomeRemoteResource();
182+ $timeout = Timer\timeout($promise, 10.0, $loop);
183+
184+ $timeout->cancel();
185+ ```
186+
187+ Note how this looks very similar to the above [ input cancellation] ( #input-cancellation )
188+ example. Accordingly, it also behaves very similar.
189+
190+ Calling ` cancel() ` on the resulting promise will merely try
191+ to ` cancel() ` the input ` $promise ` .
192+ This means that we do not take over responsibility of the outcome and it's
193+ entirely up to the input ` $promise ` to handle cancellation support.
194+
195+ The registered [ cancellation handler] ( #cancellation-handler ) is responsible for
196+ handling the ` cancel() ` call:
197+
198+ * As described above, a common use involves resource cleanup and will then * reject*
199+ the ` Promise ` .
200+ If the input ` $promise ` is being rejected, then the timeout will be aborted
201+ and the resulting promise will also be rejected.
202+ * If the input ` $promise ` is still pending, then the timout will continue
203+ running until the timer expires.
204+ The same happens if the input ` $promise ` does not register a
205+ [ cancellation handler] ( #cancellation-handler ) .
206+
207+ To re-iterate, note that calling ` cancel() ` on the resulting promise will merely
208+ try to cancel the input ` $promise ` only.
209+ It is then up to the cancellation handler of the input promise to settle the promise.
210+ If the input promise is still pending when the timeout occurs, then the normal
211+ [ timeout cancellation] ( #timeout-cancellation ) handling will trigger, effectively rejecting
212+ the output promise with a [ ` TimeoutException ` ] ( #timeoutexception ) .
213+
214+ This is done for consistency with the [ timeout cancellation] ( #timeout-cancellation )
215+ handling and also because it is assumed this is often used like this:
216+
217+ ``` php
218+ $timeout = Timer\timeout(accessSomeRemoteResource(), 10.0, $loop);
219+
220+ $timeout->cancel();
221+ ```
222+
223+ As described above, this example works as expected and cleans up any resources
224+ allocated for the input ` $promise ` .
225+
226+ Note that if the given input ` $promise ` does not support cancellation, then this
227+ is a NO-OP.
228+ This means that while the resulting promise will still be rejected after the
229+ timeout, the underlying input ` $promise ` may still be pending and can hence
230+ continue consuming resources.
231+
232+ > Note: If you're stuck on legacy versions (PHP 5.3), then the ` cancel() ` method
233+ is not available, as the Promise cancellation API is currently only available in
234+ [ react/promise v2.1.0] ( https://github.com/reactphp/promise )
235+ which in turn requires PHP 5.4 or up.
236+
176237#### Collections
177238
178239If you want to wait for multiple promises to resolve, you can use the normal promise primitives like this:
0 commit comments