Promise.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Promise; |
| 4 | |
| 5 | /** |
| 6 | * Promise represents a value that may not be available yet, but will be resolved at some point in future. |
| 7 | * It acts like a proxy to the actual value. |
| 8 | * |
| 9 | * This interface is an extension of the promises/a+ specification. |
| 10 | * |
| 11 | * @see https://promisesaplus.com/ |
| 12 | * |
| 13 | * @author Joel Wurtz <joel.wurtz@gmail.com> |
| 14 | * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 15 | */ |
| 16 | interface Promise |
| 17 | { |
| 18 | /** |
| 19 | * Promise has not been fulfilled or rejected. |
| 20 | */ |
| 21 | const PENDING = 'pending'; |
| 22 | |
| 23 | /** |
| 24 | * Promise has been fulfilled. |
| 25 | */ |
| 26 | const FULFILLED = 'fulfilled'; |
| 27 | |
| 28 | /** |
| 29 | * Promise has been rejected. |
| 30 | */ |
| 31 | const REJECTED = 'rejected'; |
| 32 | |
| 33 | /** |
| 34 | * Adds behavior for when the promise is resolved or rejected (response will be available, or error happens). |
| 35 | * |
| 36 | * If you do not care about one of the cases, you can set the corresponding callable to null |
| 37 | * The callback will be called when the value arrived and never more than once. |
| 38 | * |
| 39 | * @param callable $onFulfilled Called when a response will be available. |
| 40 | * @param callable $onRejected Called when an exception occurs. |
| 41 | * |
| 42 | * @return Promise A new resolved promise with value of the executed callback (onFulfilled / onRejected). |
| 43 | */ |
| 44 | public function then(callable $onFulfilled = null, callable $onRejected = null); |
| 45 | |
| 46 | /** |
| 47 | * Returns the state of the promise, one of PENDING, FULFILLED or REJECTED. |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | public function getState(); |
| 52 | |
| 53 | /** |
| 54 | * Wait for the promise to be fulfilled or rejected. |
| 55 | * |
| 56 | * When this method returns, the request has been resolved and if callables have been |
| 57 | * specified, the appropriate one has terminated. |
| 58 | * |
| 59 | * When $unwrap is true (the default), the response is returned, or the exception thrown |
| 60 | * on failure. Otherwise, nothing is returned or thrown. |
| 61 | * |
| 62 | * @param bool $unwrap Whether to return resolved value / throw reason or not |
| 63 | * |
| 64 | * @return mixed Resolved value, null if $unwrap is set to false |
| 65 | * |
| 66 | * @throws \Exception The rejection reason if $unwrap is set to true and the request failed. |
| 67 | */ |
| 68 | public function wait($unwrap = true); |
| 69 | } |
| 70 |