PromiseInterface.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace React\Promise;
  3. /**
  4. * @template-covariant T
  5. */
  6. interface PromiseInterface
  7. {
  8. /**
  9. * Transforms a promise's value by applying a function to the promise's fulfillment
  10. * or rejection value. Returns a new promise for the transformed result.
  11. *
  12. * The `then()` method registers new fulfilled and rejection handlers with a promise
  13. * (all parameters are optional):
  14. *
  15. * * `$onFulfilled` will be invoked once the promise is fulfilled and passed
  16. * the result as the first argument.
  17. * * `$onRejected` will be invoked once the promise is rejected and passed the
  18. * reason as the first argument.
  19. *
  20. * It returns a new promise that will fulfill with the return value of either
  21. * `$onFulfilled` or `$onRejected`, whichever is called, or will reject with
  22. * the thrown exception if either throws.
  23. *
  24. * A promise makes the following guarantees about handlers registered in
  25. * the same call to `then()`:
  26. *
  27. * 1. Only one of `$onFulfilled` or `$onRejected` will be called,
  28. * never both.
  29. * 2. `$onFulfilled` and `$onRejected` will never be called more
  30. * than once.
  31. *
  32. * @template TFulfilled
  33. * @template TRejected
  34. * @param ?(callable((T is void ? null : T)): (PromiseInterface<TFulfilled>|TFulfilled)) $onFulfilled
  35. * @param ?(callable(\Throwable): (PromiseInterface<TRejected>|TRejected)) $onRejected
  36. * @return PromiseInterface<($onRejected is null ? ($onFulfilled is null ? T : TFulfilled) : ($onFulfilled is null ? T|TRejected : TFulfilled|TRejected))>
  37. */
  38. public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;
  39. /**
  40. * Registers a rejection handler for promise. It is a shortcut for:
  41. *
  42. * ```php
  43. * $promise->then(null, $onRejected);
  44. * ```
  45. *
  46. * Additionally, you can type hint the `$reason` argument of `$onRejected` to catch
  47. * only specific errors.
  48. *
  49. * @template TThrowable of \Throwable
  50. * @template TRejected
  51. * @param callable(TThrowable): (PromiseInterface<TRejected>|TRejected) $onRejected
  52. * @return PromiseInterface<T|TRejected>
  53. */
  54. public function catch(callable $onRejected): PromiseInterface;
  55. /**
  56. * Allows you to execute "cleanup" type tasks in a promise chain.
  57. *
  58. * It arranges for `$onFulfilledOrRejected` to be called, with no arguments,
  59. * when the promise is either fulfilled or rejected.
  60. *
  61. * * If `$promise` fulfills, and `$onFulfilledOrRejected` returns successfully,
  62. * `$newPromise` will fulfill with the same value as `$promise`.
  63. * * If `$promise` fulfills, and `$onFulfilledOrRejected` throws or returns a
  64. * rejected promise, `$newPromise` will reject with the thrown exception or
  65. * rejected promise's reason.
  66. * * If `$promise` rejects, and `$onFulfilledOrRejected` returns successfully,
  67. * `$newPromise` will reject with the same reason as `$promise`.
  68. * * If `$promise` rejects, and `$onFulfilledOrRejected` throws or returns a
  69. * rejected promise, `$newPromise` will reject with the thrown exception or
  70. * rejected promise's reason.
  71. *
  72. * `finally()` behaves similarly to the synchronous finally statement. When combined
  73. * with `catch()`, `finally()` allows you to write code that is similar to the familiar
  74. * synchronous catch/finally pair.
  75. *
  76. * Consider the following synchronous code:
  77. *
  78. * ```php
  79. * try {
  80. * return doSomething();
  81. * } catch(\Exception $e) {
  82. * return handleError($e);
  83. * } finally {
  84. * cleanup();
  85. * }
  86. * ```
  87. *
  88. * Similar asynchronous code (with `doSomething()` that returns a promise) can be
  89. * written:
  90. *
  91. * ```php
  92. * return doSomething()
  93. * ->catch('handleError')
  94. * ->finally('cleanup');
  95. * ```
  96. *
  97. * @param callable(): (void|PromiseInterface<void>) $onFulfilledOrRejected
  98. * @return PromiseInterface<T>
  99. */
  100. public function finally(callable $onFulfilledOrRejected): PromiseInterface;
  101. /**
  102. * The `cancel()` method notifies the creator of the promise that there is no
  103. * further interest in the results of the operation.
  104. *
  105. * Once a promise is settled (either fulfilled or rejected), calling `cancel()` on
  106. * a promise has no effect.
  107. *
  108. * @return void
  109. */
  110. public function cancel(): void;
  111. /**
  112. * [Deprecated] Registers a rejection handler for a promise.
  113. *
  114. * This method continues to exist only for BC reasons and to ease upgrading
  115. * between versions. It is an alias for:
  116. *
  117. * ```php
  118. * $promise->catch($onRejected);
  119. * ```
  120. *
  121. * @template TThrowable of \Throwable
  122. * @template TRejected
  123. * @param callable(TThrowable): (PromiseInterface<TRejected>|TRejected) $onRejected
  124. * @return PromiseInterface<T|TRejected>
  125. * @deprecated 3.0.0 Use catch() instead
  126. * @see self::catch()
  127. */
  128. public function otherwise(callable $onRejected): PromiseInterface;
  129. /**
  130. * [Deprecated] Allows you to execute "cleanup" type tasks in a promise chain.
  131. *
  132. * This method continues to exist only for BC reasons and to ease upgrading
  133. * between versions. It is an alias for:
  134. *
  135. * ```php
  136. * $promise->finally($onFulfilledOrRejected);
  137. * ```
  138. *
  139. * @param callable(): (void|PromiseInterface<void>) $onFulfilledOrRejected
  140. * @return PromiseInterface<T>
  141. * @deprecated 3.0.0 Use finally() instead
  142. * @see self::finally()
  143. */
  144. public function always(callable $onFulfilledOrRejected): PromiseInterface;
  145. }