Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ Table of Contents
* [PromiseInterface](#promiseinterface)
* [PromiseInterface::then()](#promiseinterfacethen)
* [PromiseInterface::done()](#promiseinterfacedone)
* [PromiseInterface::otherwise()](#promiseinterfaceotherwise)
* [PromiseInterface::always()](#promiseinterfacealways)
* [~~PromiseInterface::otherwise()~~](#promiseinterfaceotherwise)
* [PromiseInterface::catch()](#promiseinterfacecatch)
* [~~PromiseInterface::always()~~](#promiseinterfacealways)
* [PromiseInterface::finally()](#promiseinterfacefinally)
* [PromiseInterface::cancel()](#promiseinterfacecancel)
* [Promise](#promise-2)
* [Functions](#functions)
Expand Down Expand Up @@ -206,10 +208,14 @@ Since the purpose of `done()` is consumption rather than transformation,
* [PromiseInterface::then()](#promiseinterfacethen)
* [done() vs. then()](#done-vs-then)

#### PromiseInterface::otherwise()
#### ~~PromiseInterface::otherwise()~~

The `otherwise` method has been deprecated in favour of [`catch`](#promiseinterfacecatch)

#### PromiseInterface::catch()

```php
$promise->otherwise(callable $onRejected);
$promise->catch(callable $onRejected);
```

Registers a rejection handler for promise. It is a shortcut for:
Expand All @@ -223,19 +229,23 @@ only specific errors.

```php
$promise
->otherwise(function (\RuntimeException $reason) {
->catch(function (\RuntimeException $reason) {
// Only catch \RuntimeException instances
// All other types of errors will propagate automatically
})
->otherwise(function (\Throwable $reason) {
->catch(function (\Throwable $reason) {
// Catch other errors
});
```

#### PromiseInterface::always()
#### ~~PromiseInterface::always()~~

The `otherwise` method has been deprecated in favour of [`finally`](#promiseinterfacefinally)

#### PromiseInterface::finally()

```php
$newPromise = $promise->always(callable $onFulfilledOrRejected);
$newPromise = $promise->finally(callable $onFulfilledOrRejected);
```

Allows you to execute "cleanup" type tasks in a promise chain.
Expand All @@ -254,8 +264,8 @@ when the promise is either fulfilled or rejected.
rejected promise, `$newPromise` will reject with the thrown exception or
rejected promise's reason.

`always()` behaves similarly to the synchronous finally statement. When combined
with `otherwise()`, `always()` allows you to write code that is similar to the familiar
`finally()` behaves similarly to the synchronous finally statement. When combined
with `catch()`, `finally()` allows you to write code that is similar to the familiar
synchronous catch/finally pair.

Consider the following synchronous code:
Expand All @@ -275,8 +285,8 @@ written:

```php
return doSomething()
->otherwise('handleError')
->always('cleanup');
->catch('handleError')
->finally('cleanup');
```

#### PromiseInterface::cancel()
Expand Down Expand Up @@ -559,17 +569,17 @@ $deferred->promise()
->then(function ($x) {
throw new \Exception($x + 1);
})
->otherwise(function (\Exception $x) {
->catch(function (\Exception $x) {
// Propagate the rejection
throw $x;
})
->otherwise(function (\Exception $x) {
->catch(function (\Exception $x) {
// Can also propagate by returning another rejection
return React\Promise\reject(
new \Exception($x->getMessage() + 1)
);
})
->otherwise(function ($x) {
->catch(function ($x) {
echo 'Reject ' . $x->getMessage(); // 3
});

Expand All @@ -591,7 +601,7 @@ $deferred->promise()
->then(function ($x) {
throw new \Exception($x + 1);
})
->otherwise(function (\Exception $x) {
->catch(function (\Exception $x) {
// Handle the rejection, and don't propagate.
// This is like catch without a rethrow
return $x->getMessage() + 1;
Expand Down
16 changes: 16 additions & 0 deletions src/Internal/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,28 @@ public function done(callable $onFulfilled = null, callable $onRejected = null):
});
}

/**
* @deprecated Use catch instead
*/
public function otherwise(callable $onRejected): PromiseInterface
{
return $this->catch($onRejected);
}

public function catch(callable $onRejected): PromiseInterface
{
return $this;
}

/**
* @deprecated Use finally instead
*/
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->finally($onFulfilledOrRejected);
}

public function finally(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(function ($value) use ($onFulfilledOrRejected): PromiseInterface {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
Expand Down
16 changes: 16 additions & 0 deletions src/Internal/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ public function done(callable $onFulfilled = null, callable $onRejected = null):
});
}

/**
* @deprecated Use catch instead
*/
public function otherwise(callable $onRejected): PromiseInterface
{
return $this->catch($onRejected);
}

public function catch(callable $onRejected): PromiseInterface
{
if (!_checkTypehint($onRejected, $this->reason)) {
return $this;
Expand All @@ -70,7 +78,15 @@ public function otherwise(callable $onRejected): PromiseInterface
return $this->then(null, $onRejected);
}

/**
* @deprecated Use finally instead
*/
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->finally($onFulfilledOrRejected);
}

public function finally(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected): PromiseInterface {
return resolve($onFulfilledOrRejected())->then(function () use ($reason): PromiseInterface {
Expand Down
16 changes: 16 additions & 0 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ public function done(callable $onFulfilled = null, callable $onRejected = null):
};
}

/**
* @deprecated Use catch instead
*/
public function otherwise(callable $onRejected): PromiseInterface
{
return $this->catch($onRejected);
}

public function catch(callable $onRejected): PromiseInterface
{
return $this->then(null, static function ($reason) use ($onRejected) {
if (!_checkTypehint($onRejected, $reason)) {
Expand All @@ -81,7 +89,15 @@ public function otherwise(callable $onRejected): PromiseInterface
});
}

/**
* @deprecated Use finally instead
*/
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->finally($onFulfilledOrRejected);
}

public function finally(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(static function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
Expand Down
10 changes: 9 additions & 1 deletion src/PromiseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function then(?callable $onFulfilled = null, ?callable $onRejected = null
*/
public function done(callable $onFulfilled = null, callable $onRejected = null): void;

/**
* @see catch
* @deprecated Use catch instead
* @param callable $onRejected
* @return PromiseInterface
*/
public function otherwise(callable $onRejected): PromiseInterface;

/**
* Registers a rejection handler for promise. It is a shortcut for:
*
Expand All @@ -63,7 +71,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null):
* @param callable $onRejected
* @return PromiseInterface
*/
public function otherwise(callable $onRejected): PromiseInterface;
public function catch(callable $onRejected): PromiseInterface;

/**
* Allows you to execute "cleanup" type tasks in a promise chain.
Expand Down
4 changes: 2 additions & 2 deletions tests/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->otherwise(function () { });
$promise->catch(function () { });
unset($promise);

$this->assertSame(0, gc_collect_cycles());
Expand All @@ -239,7 +239,7 @@ public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPro
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->always(function () { });
$promise->finally(function () { });
unset($promise);

$this->assertSame(0, gc_collect_cycles());
Expand Down
12 changes: 6 additions & 6 deletions tests/PromiseTest/PromiseFulfilledTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function otherwiseShouldNotInvokeRejectionHandlerForFulfilledPromise()
$adapter = $this->getPromiseTestAdapter();

$adapter->resolve(1);
$adapter->promise()->otherwise($this->expectCallableNever());
$adapter->promise()->catch($this->expectCallableNever());
}

/** @test */
Expand All @@ -284,7 +284,7 @@ public function alwaysShouldNotSuppressValueForFulfilledPromise()

$adapter->resolve($value);
$adapter->promise()
->always(function () {})
->finally(function () {})
->then($mock);
}

Expand All @@ -303,7 +303,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulf

$adapter->resolve($value);
$adapter->promise()
->always(function () {
->finally(function () {
return 1;
})
->then($mock);
Expand All @@ -324,7 +324,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfill

$adapter->resolve($value);
$adapter->promise()
->always(function () {
->finally(function () {
return resolve(1);
})
->then($mock);
Expand All @@ -345,7 +345,7 @@ public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise()

$adapter->resolve(1);
$adapter->promise()
->always(function () use ($exception) {
->finally(function () use ($exception) {
throw $exception;
})
->then(null, $mock);
Expand All @@ -366,7 +366,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise()

$adapter->resolve(1);
$adapter->promise()
->always(function () use ($exception) {
->finally(function () use ($exception) {
return reject($exception);
})
->then(null, $mock);
Expand Down
4 changes: 2 additions & 2 deletions tests/PromiseTest/PromisePendingTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public function otherwiseShouldNotInvokeRejectionHandlerForPendingPromise()
$adapter = $this->getPromiseTestAdapter();

$adapter->settle();
$adapter->promise()->otherwise($this->expectCallableNever());
$adapter->promise()->catch($this->expectCallableNever());
}

/** @test */
public function alwaysShouldReturnAPromiseForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();

self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->always(function () {}));
self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->finally(function () {}));
}
}
18 changes: 9 additions & 9 deletions tests/PromiseTest/PromiseRejectedTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public function otherwiseShouldInvokeRejectionHandlerForRejectedPromise()
->with($this->identicalTo($exception));

$adapter->reject($exception);
$adapter->promise()->otherwise($mock);
$adapter->promise()->catch($mock);
}

/** @test */
Expand All @@ -343,7 +343,7 @@ public function otherwiseShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnEx

$adapter->reject($exception);
$adapter->promise()
->otherwise(function ($reason) use ($mock) {
->catch(function ($reason) use ($mock) {
$mock($reason);
});
}
Expand All @@ -363,7 +363,7 @@ public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForR

$adapter->reject($exception);
$adapter->promise()
->otherwise(function (InvalidArgumentException $reason) use ($mock) {
->catch(function (InvalidArgumentException $reason) use ($mock) {
$mock($reason);
});
}
Expand All @@ -379,7 +379,7 @@ public function otherwiseShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchType

$adapter->reject($exception);
$adapter->promise()
->otherwise(function (InvalidArgumentException $reason) use ($mock) {
->catch(function (InvalidArgumentException $reason) use ($mock) {
$mock($reason);
});
}
Expand All @@ -399,7 +399,7 @@ public function alwaysShouldNotSuppressRejectionForRejectedPromise()

$adapter->reject($exception);
$adapter->promise()
->always(function () {})
->finally(function () {})
->then(null, $mock);
}

Expand All @@ -418,7 +418,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseFor

$adapter->reject($exception);
$adapter->promise()
->always(function () {
->finally(function () {
return 1;
})
->then(null, $mock);
Expand All @@ -439,7 +439,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRej

$adapter->reject($exception);
$adapter->promise()
->always(function () {
->finally(function () {
return resolve(1);
})
->then(null, $mock);
Expand All @@ -461,7 +461,7 @@ public function alwaysShouldRejectWhenHandlerThrowsForRejectedPromise()

$adapter->reject($exception1);
$adapter->promise()
->always(function () use ($exception2) {
->finally(function () use ($exception2) {
throw $exception2;
})
->then(null, $mock);
Expand All @@ -483,7 +483,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForRejectedPromise()

$adapter->reject($exception1);
$adapter->promise()
->always(function () use ($exception2) {
->finally(function () use ($exception2) {
return reject($exception2);
})
->then(null, $mock);
Expand Down
Loading