|
5 | 5 | interface PromiseInterface |
6 | 6 | { |
7 | 7 | /** |
| 8 | + * Transforms a promise's value by applying a function to the promise's fulfillment |
| 9 | + * or rejection value. Returns a new promise for the transformed result. |
| 10 | + * |
| 11 | + * The `then()` method registers new fulfilled and rejection handlers with a promise |
| 12 | + * (all parameters are optional): |
| 13 | + * |
| 14 | + * * `$onFulfilled` will be invoked once the promise is fulfilled and passed |
| 15 | + * the result as the first argument. |
| 16 | + * * `$onRejected` will be invoked once the promise is rejected and passed the |
| 17 | + * reason as the first argument. |
| 18 | + * |
| 19 | + * It returns a new promise that will fulfill with the return value of either |
| 20 | + * `$onFulfilled` or `$onRejected`, whichever is called, or will reject with |
| 21 | + * the thrown exception if either throws. |
| 22 | + * |
| 23 | + * A promise makes the following guarantees about handlers registered in |
| 24 | + * the same call to `then()`: |
| 25 | + * |
| 26 | + * 1. Only one of `$onFulfilled` or `$onRejected` will be called, |
| 27 | + * never both. |
| 28 | + * 2. `$onFulfilled` and `$onRejected` will never be called more |
| 29 | + * than once. |
| 30 | + * |
| 31 | + * @param callable|null $onFulfilled |
| 32 | + * @param callable|null $onRejected |
8 | 33 | * @return PromiseInterface |
9 | 34 | */ |
10 | 35 | public function then(callable $onFulfilled = null, callable $onRejected = null); |
11 | 36 |
|
12 | 37 | /** |
| 38 | + * Consumes the promise's ultimate value if the promise fulfills, or handles the |
| 39 | + * ultimate error. |
| 40 | + * |
| 41 | + * It will cause a fatal error (`E_USER_ERROR`) if either `$onFulfilled` or |
| 42 | + * `$onRejected` throw or return a rejected promise. |
| 43 | + * |
| 44 | + * Since the purpose of `done()` is consumption rather than transformation, |
| 45 | + * `done()` always returns `null`. |
| 46 | + * |
| 47 | + * @param callable|null $onFulfilled |
| 48 | + * @param callable|null $onRejected |
13 | 49 | * @return void |
14 | 50 | */ |
15 | 51 | public function done(callable $onFulfilled = null, callable $onRejected = null); |
16 | 52 |
|
17 | 53 | /** |
| 54 | + * Registers a rejection handler for promise. It is a shortcut for: |
| 55 | + * |
| 56 | + * ```php |
| 57 | + * $promise->then(null, $onRejected); |
| 58 | + * ``` |
| 59 | + * |
| 60 | + * Additionally, you can type hint the `$reason` argument of `$onRejected` to catch |
| 61 | + * only specific errors. |
| 62 | + * |
| 63 | + * @param callable $onRejected |
18 | 64 | * @return PromiseInterface |
19 | 65 | */ |
20 | 66 | public function otherwise(callable $onRejected); |
21 | 67 |
|
22 | 68 | /** |
| 69 | + * Allows you to execute "cleanup" type tasks in a promise chain. |
| 70 | + * |
| 71 | + * It arranges for `$onFulfilledOrRejected` to be called, with no arguments, |
| 72 | + * when the promise is either fulfilled or rejected. |
| 73 | + * |
| 74 | + * * If `$promise` fulfills, and `$onFulfilledOrRejected` returns successfully, |
| 75 | + * `$newPromise` will fulfill with the same value as `$promise`. |
| 76 | + * * If `$promise` fulfills, and `$onFulfilledOrRejected` throws or returns a |
| 77 | + * rejected promise, `$newPromise` will reject with the thrown exception or |
| 78 | + * rejected promise's reason. |
| 79 | + * * If `$promise` rejects, and `$onFulfilledOrRejected` returns successfully, |
| 80 | + * `$newPromise` will reject with the same reason as `$promise`. |
| 81 | + * * If `$promise` rejects, and `$onFulfilledOrRejected` throws or returns a |
| 82 | + * rejected promise, `$newPromise` will reject with the thrown exception or |
| 83 | + * rejected promise's reason. |
| 84 | + * |
| 85 | + * `always()` behaves similarly to the synchronous finally statement. When combined |
| 86 | + * with `otherwise()`, `always()` allows you to write code that is similar to the familiar |
| 87 | + * synchronous catch/finally pair. |
| 88 | + * |
| 89 | + * Consider the following synchronous code: |
| 90 | + * |
| 91 | + * ```php |
| 92 | + * try { |
| 93 | + * return doSomething(); |
| 94 | + * } catch(\Exception $e) { |
| 95 | + * return handleError($e); |
| 96 | + * } finally { |
| 97 | + * cleanup(); |
| 98 | + * } |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * Similar asynchronous code (with `doSomething()` that returns a promise) can be |
| 102 | + * written: |
| 103 | + * |
| 104 | + * ```php |
| 105 | + * return doSomething() |
| 106 | + * ->otherwise('handleError') |
| 107 | + * ->always('cleanup'); |
| 108 | + * ``` |
| 109 | + * |
| 110 | + * @param callable $onFulfilledOrRejected |
23 | 111 | * @return PromiseInterface |
24 | 112 | */ |
25 | 113 | public function always(callable $onFulfilledOrRejected); |
26 | 114 |
|
27 | 115 | /** |
| 116 | + * The `cancel()` method notifies the creator of the promise that there is no |
| 117 | + * further interest in the results of the operation. |
| 118 | + * |
| 119 | + * Once a promise is settled (either fulfilled or rejected), calling `cancel()` on |
| 120 | + * a promise has no effect. |
| 121 | + * |
28 | 122 | * @return void |
29 | 123 | */ |
30 | 124 | public function cancel(); |
|
0 commit comments