@@ -37,6 +37,8 @@ Table of Contents
3737 * [ When::reject()] ( #whenreject )
3838 * [ When::lazy()] ( #whenlazy )
3939 * [ Promisor] ( #promisor )
40+ * [ CancellablePromiseInterface] ( #cancellablepromiseinterface )
41+ * [ CancellablePromiseInterface::cancel()] ( #cancellablepromiseinterfacecancel )
40424 . [ Examples] ( #examples )
4143 * [ How to use Deferred] ( #how-to-use-deferred )
4244 * [ How Promise forwarding works] ( #how-promise-forwarding-works )
@@ -111,6 +113,18 @@ $deferred->reject(mixed $reason = null);
111113$deferred->progress(mixed $update = null);
112114```
113115
116+ The constructor of the ` Deferred ` accepts an optional ` $canceller ` argument.
117+ See [ Promise] ( #promise-1 ) for more information.
118+
119+
120+ ``` php
121+ $deferred = new React\Promise\Deferred(function ($resolve, $reject, $progress) {
122+ throw new \Exception('Promise cancelled');
123+ });
124+
125+ $deferred->cancel();
126+ ```
127+
114128### Promise
115129
116130The Promise represents the eventual outcome, which is either fulfillment
@@ -133,7 +147,13 @@ $resolver = function (callable $resolve, callable $reject, callable $notify) {
133147 // or $notify($progressNotification);
134148};
135149
136- $promise = new React\Promise\Promise($resolver);
150+ $canceller = function (callable $resolve, callable $reject, callable $progress) {
151+ // Cancel/abort any running operations like network connections, streams etc.
152+
153+ $reject(new \Exception('Promise cancelled'));
154+ };
155+
156+ $promise = new React\Promise\Promise($resolver, $canceller);
137157```
138158
139159The promise constructor receives a resolver function which will be called
@@ -150,6 +170,9 @@ immediately with 3 arguments:
150170If the resolver throws an exception, the promise will be rejected with that
151171thrown exception as the rejection reason.
152172
173+ The resolver function will be called immediately, the canceller function only
174+ once all consumers called the ` cancel() ` method of the promise.
175+
153176A Promise has a single method ` then() ` which registers new fulfilled, error and
154177progress handlers with this Promise (all parameters are optional):
155178
@@ -484,6 +507,32 @@ The `React\Promise\PromisorInterface` provides a common interface for objects
484507that provide a promise. ` React\Promise\Deferred ` implements it, but since it
485508is part of the public API anyone can implement it.
486509
510+ ### CancellablePromiseInterface
511+
512+ A cancellable promise provides a mechanism for consumers to notify the creator
513+ of the promise that they are not longer interested in the result of an
514+ operation.
515+
516+ #### CancellablePromiseInterface::cancel()
517+
518+ ``` php
519+ $promise->cancel();
520+ ```
521+
522+ The ` cancel() ` method notifies the creator of the promise that there is no
523+ further interest in the results of the operation.
524+
525+ Once a promise is settled (either resolved or rejected), calling ` cancel() ` on
526+ a promise has no effect.
527+
528+ #### Implementations
529+
530+ * [ Deferred] ( #deferred-1 )
531+ * [ Promise] ( #promise-1 )
532+ * [ FulfilledPromise] ( #fulfilledpromise )
533+ * [ RejectedPromise] ( #rejectedpromise )
534+ * [ LazyPromise] ( #lazypromise )
535+
487536Examples
488537--------
489538
0 commit comments