@@ -29,7 +29,6 @@ Table of Contents
2929 * [ Deferred::reject()] ( #deferredreject )
3030 * [ PromiseInterface] ( #promiseinterface )
3131 * [ PromiseInterface::then()] ( #promiseinterfacethen )
32- * [ PromiseInterface::done()] ( #promiseinterfacedone )
3332 * [ PromiseInterface::catch()] ( #promiseinterfacecatch )
3433 * [ PromiseInterface::finally()] ( #promiseinterfacefinally )
3534 * [ PromiseInterface::cancel()] ( #promiseinterfacecancel )
@@ -48,7 +47,6 @@ Table of Contents
4847 * [ Resolution forwarding] ( #resolution-forwarding )
4948 * [ Rejection forwarding] ( #rejection-forwarding )
5049 * [ Mixed resolution and rejection forwarding] ( #mixed-resolution-and-rejection-forwarding )
51- * [ done() vs. then()] ( #done-vs-then )
52505 . [ Install] ( #install )
53516 . [ Credits] ( #credits )
54527 . [ License] ( #license )
@@ -183,28 +181,6 @@ the same call to `then()`:
183181
184182* [ resolve()] ( #resolve ) - Creating a resolved promise
185183* [ reject()] ( #reject ) - Creating a rejected promise
186- * [ PromiseInterface::done()] ( #promiseinterfacedone )
187- * [ done() vs. then()] ( #done-vs-then )
188-
189- #### PromiseInterface::done()
190-
191- ``` php
192- $promise->done(callable $onFulfilled = null, callable $onRejected = null);
193- ```
194-
195- Consumes the promise's ultimate value if the promise fulfills, or handles the
196- ultimate error.
197-
198- It will cause a fatal error (` E_USER_ERROR ` ) if either ` $onFulfilled ` or
199- ` $onRejected ` throw or return a rejected promise.
200-
201- Since the purpose of ` done() ` is consumption rather than transformation,
202- ` done() ` always returns ` null ` .
203-
204- #### See also
205-
206- * [ PromiseInterface::then()] ( #promiseinterfacethen )
207- * [ done() vs. then()] ( #done-vs-then )
208184
209185#### PromiseInterface::catch()
210186
@@ -579,74 +555,6 @@ $deferred->promise()
579555$deferred->resolve(1); // Prints "Mixed 4"
580556```
581557
582- ### done() vs. then()
583-
584- The golden rule is:
585-
586- Either return your promise, or call done() on it.
587-
588- At a first glance, ` then() ` and ` done() ` seem very similar. However, there are
589- important distinctions.
590-
591- The intent of ` then() ` is to transform a promise's value and to pass or return
592- a new promise for the transformed value along to other parts of your code.
593-
594- The intent of ` done() ` is to consume a promise's value, transferring
595- responsibility for the value to your code.
596-
597- In addition to transforming a value, ` then() ` allows you to recover from, or
598- propagate intermediate errors. Any errors that are not handled will be caught
599- by the promise machinery and used to reject the promise returned by ` then() ` .
600-
601- Calling ` done() ` transfers all responsibility for errors to your code. If an
602- error (either a thrown exception or returned rejection) escapes the
603- ` $onFulfilled ` or ` $onRejected ` callbacks you provide to ` done() ` , it will cause
604- a fatal error.
605-
606- ``` php
607- function getJsonResult()
608- {
609- return queryApi()
610- ->then(
611- // Transform API results to an object
612- function ($jsonResultString) {
613- return json_decode($jsonResultString);
614- },
615- // Transform API errors to an exception
616- function ($jsonErrorString) {
617- $object = json_decode($jsonErrorString);
618- throw new ApiErrorException($object->errorMessage);
619- }
620- );
621- }
622-
623- // Here we provide no rejection handler. If the promise returned has been
624- // rejected, the ApiErrorException will be thrown
625- getJsonResult()
626- ->done(
627- // Consume transformed object
628- function ($jsonResultObject) {
629- // Do something with $jsonResultObject
630- }
631- );
632-
633- // Here we provide a rejection handler which will either throw while debugging
634- // or log the exception
635- getJsonResult()
636- ->done(
637- function ($jsonResultObject) {
638- // Do something with $jsonResultObject
639- },
640- function (ApiErrorException $exception) {
641- if (isDebug()) {
642- throw $exception;
643- } else {
644- logException($exception);
645- }
646- }
647- );
648- ```
649-
650558Install
651559-------
652560
0 commit comments