@@ -29,7 +29,6 @@ Table of Contents
29
29
* [ Deferred::reject()] ( #deferredreject )
30
30
* [ PromiseInterface] ( #promiseinterface )
31
31
* [ PromiseInterface::then()] ( #promiseinterfacethen )
32
- * [ PromiseInterface::done()] ( #promiseinterfacedone )
33
32
* [ PromiseInterface::catch()] ( #promiseinterfacecatch )
34
33
* [ PromiseInterface::finally()] ( #promiseinterfacefinally )
35
34
* [ PromiseInterface::cancel()] ( #promiseinterfacecancel )
@@ -48,7 +47,6 @@ Table of Contents
48
47
* [ Resolution forwarding] ( #resolution-forwarding )
49
48
* [ Rejection forwarding] ( #rejection-forwarding )
50
49
* [ Mixed resolution and rejection forwarding] ( #mixed-resolution-and-rejection-forwarding )
51
- * [ done() vs. then()] ( #done-vs-then )
52
50
5 . [ Install] ( #install )
53
51
6 . [ Credits] ( #credits )
54
52
7 . [ License] ( #license )
@@ -183,28 +181,6 @@ the same call to `then()`:
183
181
184
182
* [ resolve()] ( #resolve ) - Creating a resolved promise
185
183
* [ 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 )
208
184
209
185
#### PromiseInterface::catch()
210
186
@@ -579,74 +555,6 @@ $deferred->promise()
579
555
$deferred->resolve(1); // Prints "Mixed 4"
580
556
```
581
557
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
-
650
558
Install
651
559
-------
652
560
0 commit comments