@@ -29,9 +29,11 @@ Table of Contents
29
29
* [ PromiseInterface] ( #promiseinterface )
30
30
* [ PromiseInterface::then()] ( #promiseinterfacethen )
31
31
* [ PromiseInterface::done()] ( #promiseinterfacedone )
32
- * [ PromiseInterface::otherwise ()] ( #promiseinterfaceotherwise )
33
- * [ PromiseInterface::always ()] ( #promiseinterfacealways )
32
+ * [ PromiseInterface::catch ()] ( #promiseinterfacecatch )
33
+ * [ PromiseInterface::finally ()] ( #promiseinterfacefinally )
34
34
* [ PromiseInterface::cancel()] ( #promiseinterfacecancel )
35
+ * [ ~~ PromiseInterface::otherwise()~~ ] ( #promiseinterfaceotherwise )
36
+ * [ ~~ PromiseInterface::always()~~ ] ( #promiseinterfacealways )
35
37
* [ Promise] ( #promise-2 )
36
38
* [ Functions] ( #functions )
37
39
* [ resolve()] ( #resolve )
@@ -206,10 +208,10 @@ Since the purpose of `done()` is consumption rather than transformation,
206
208
* [ PromiseInterface::then()] ( #promiseinterfacethen )
207
209
* [ done() vs. then()] ( #done-vs-then )
208
210
209
- #### PromiseInterface::otherwise ()
211
+ #### PromiseInterface::catch ()
210
212
211
213
``` php
212
- $promise->otherwise (callable $onRejected);
214
+ $promise->catch (callable $onRejected);
213
215
```
214
216
215
217
Registers a rejection handler for promise. It is a shortcut for:
@@ -223,19 +225,19 @@ only specific errors.
223
225
224
226
``` php
225
227
$promise
226
- ->otherwise (function (\RuntimeException $reason) {
228
+ ->catch (function (\RuntimeException $reason) {
227
229
// Only catch \RuntimeException instances
228
230
// All other types of errors will propagate automatically
229
231
})
230
- ->otherwise (function (\Throwable $reason) {
232
+ ->catch (function (\Throwable $reason) {
231
233
// Catch other errors
232
234
});
233
235
```
234
236
235
- #### PromiseInterface::always ()
237
+ #### PromiseInterface::finally ()
236
238
237
239
``` php
238
- $newPromise = $promise->always (callable $onFulfilledOrRejected);
240
+ $newPromise = $promise->finally (callable $onFulfilledOrRejected);
239
241
```
240
242
241
243
Allows you to execute "cleanup" type tasks in a promise chain.
@@ -254,15 +256,15 @@ when the promise is either fulfilled or rejected.
254
256
rejected promise, ` $newPromise ` will reject with the thrown exception or
255
257
rejected promise's reason.
256
258
257
- ` always ()` behaves similarly to the synchronous finally statement. When combined
258
- with ` otherwise ()` , ` always ()` allows you to write code that is similar to the familiar
259
+ ` finally ()` behaves similarly to the synchronous finally statement. When combined
260
+ with ` catch ()` , ` finally ()` allows you to write code that is similar to the familiar
259
261
synchronous catch/finally pair.
260
262
261
263
Consider the following synchronous code:
262
264
263
265
``` php
264
266
try {
265
- return doSomething();
267
+ return doSomething();
266
268
} catch (\Throwable $e) {
267
269
return handleError($e);
268
270
} finally {
@@ -275,8 +277,8 @@ written:
275
277
276
278
``` php
277
279
return doSomething()
278
- ->otherwise ('handleError')
279
- ->always ('cleanup');
280
+ ->catch ('handleError')
281
+ ->finally ('cleanup');
280
282
```
281
283
282
284
#### PromiseInterface::cancel()
@@ -291,6 +293,32 @@ further interest in the results of the operation.
291
293
Once a promise is settled (either fulfilled or rejected), calling ` cancel() ` on
292
294
a promise has no effect.
293
295
296
+ #### ~~ PromiseInterface::otherwise()~~
297
+
298
+ > Deprecated since v3.0.0, see [ ` catch() ` ] ( #promiseinterfacecatch ) instead.
299
+
300
+ The ` otherwise() ` method registers a rejection handler for a promise.
301
+
302
+ This method continues to exist only for BC reasons and to ease upgrading
303
+ between versions. It is an alias for:
304
+
305
+ ``` php
306
+ $promise->catch($onRejected);
307
+ ```
308
+
309
+ #### ~~ PromiseInterface::always()~~
310
+
311
+ > Deprecated since v3.0.0, see [ ` finally() ` ] ( #promiseinterfacefinally ) instead.
312
+
313
+ The ` always() ` method allows you to execute "cleanup" type tasks in a promise chain.
314
+
315
+ This method continues to exist only for BC reasons and to ease upgrading
316
+ between versions. It is an alias for:
317
+
318
+ ``` php
319
+ $promise->finally($onFulfilledOrRejected);
320
+ ```
321
+
294
322
### Promise
295
323
296
324
Creates a promise whose state is controlled by the functions passed to
@@ -559,17 +587,17 @@ $deferred->promise()
559
587
->then(function ($x) {
560
588
throw new \Exception($x + 1);
561
589
})
562
- ->otherwise (function (\Exception $x) {
590
+ ->catch (function (\Exception $x) {
563
591
// Propagate the rejection
564
592
throw $x;
565
593
})
566
- ->otherwise (function (\Exception $x) {
594
+ ->catch (function (\Exception $x) {
567
595
// Can also propagate by returning another rejection
568
596
return React\Promise\reject(
569
597
new \Exception($x->getMessage() + 1)
570
598
);
571
599
})
572
- ->otherwise (function ($x) {
600
+ ->catch (function ($x) {
573
601
echo 'Reject ' . $x->getMessage(); // 3
574
602
});
575
603
@@ -591,7 +619,7 @@ $deferred->promise()
591
619
->then(function ($x) {
592
620
throw new \Exception($x + 1);
593
621
})
594
- ->otherwise (function (\Exception $x) {
622
+ ->catch (function (\Exception $x) {
595
623
// Handle the rejection, and don't propagate.
596
624
// This is like catch without a rethrow
597
625
return $x->getMessage() + 1;
0 commit comments