function PromiseInterface::finally
Allows you to execute "cleanup" type tasks in a promise chain.
It arranges for `$onFulfilledOrRejected` to be called, with no arguments, when the promise is either fulfilled or rejected.
- If `$promise` fulfills, and `$onFulfilledOrRejected` returns successfully, `$newPromise` will fulfill with the same value as `$promise`.
- If `$promise` fulfills, and `$onFulfilledOrRejected` throws or returns a rejected promise, `$newPromise` will reject with the thrown exception or rejected promise's reason.
- If `$promise` rejects, and `$onFulfilledOrRejected` returns successfully, `$newPromise` will reject with the same reason as `$promise`.
- If `$promise` rejects, and `$onFulfilledOrRejected` throws or returns a rejected promise, `$newPromise` will reject with the thrown exception or rejected promise's reason.
`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:
```php try { return doSomething(); } catch(\Exception $e) { return handleError($e); } finally { cleanup(); } ```
Similar asynchronous code (with `doSomething()` that returns a promise) can be written:
```php return doSomething() ->catch('handleError') ->finally('cleanup'); ```
Parameters
callable(): (void|PromiseInterface<void>) $onFulfilledOrRejected:
Return value
PromiseInterface<T>
3 methods override PromiseInterface::finally()
- FulfilledPromise::finally in vendor/
react/ promise/ src/ Internal/ FulfilledPromise.php - Allows you to execute "cleanup" type tasks in a promise chain.
- Promise::finally in vendor/
react/ promise/ src/ Promise.php - Allows you to execute "cleanup" type tasks in a promise chain.
- RejectedPromise::finally in vendor/
react/ promise/ src/ Internal/ RejectedPromise.php - Allows you to execute "cleanup" type tasks in a promise chain.
File
-
vendor/
react/ promise/ src/ PromiseInterface.php, line 104
Class
- PromiseInterface
- @template-covariant T
Namespace
React\PromiseCode
public function finally(callable $onFulfilledOrRejected) : PromiseInterface;