2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 05:48:28 +02:00
|
|
|
namespace Amp;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-06-01 19:18:11 +02:00
|
|
|
/**
|
2017-04-26 19:20:30 +02:00
|
|
|
* Creates a failed promise using the given exception.
|
2020-03-28 13:52:48 +01:00
|
|
|
*
|
2020-03-28 20:27:42 +01:00
|
|
|
* @template-covariant TValue
|
|
|
|
* @template-implements Promise<TValue>
|
2016-06-01 19:18:11 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
final class Failure implements Promise
|
|
|
|
{
|
2020-09-24 18:52:22 +02:00
|
|
|
private \Throwable $exception;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @param \Throwable $exception Rejection reason.
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __construct(\Throwable $exception)
|
|
|
|
{
|
2016-05-21 16:44:52 +02:00
|
|
|
$this->exception = $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
public function onResolve(callable $onResolved): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-24 18:52:22 +02:00
|
|
|
Loop::defer(function () use ($onResolved): void {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var mixed $result */
|
2017-03-28 01:37:55 +02:00
|
|
|
$result = $onResolved($this->exception, null);
|
|
|
|
|
|
|
|
if ($result === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:17:52 +01:00
|
|
|
if ($result instanceof Promise) {
|
2017-03-28 01:37:55 +02:00
|
|
|
Promise\rethrow($result);
|
|
|
|
}
|
2020-09-24 18:52:22 +02:00
|
|
|
});
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
2016-05-22 20:43:37 +02:00
|
|
|
}
|