1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 09:27:46 +01:00
amp/lib/Failure.php
2018-06-18 20:00:01 +02:00

49 lines
1.1 KiB
PHP

<?php
namespace Amp;
use React\Promise\PromiseInterface as ReactPromise;
/**
* Creates a failed promise using the given exception.
*/
final class Failure implements Promise
{
/** @var \Throwable $exception */
private $exception;
/**
* @param \Throwable $exception Rejection reason.
*/
public function __construct(\Throwable $exception)
{
$this->exception = $exception;
}
/**
* {@inheritdoc}
*/
public function onResolve(callable $onResolved)
{
try {
$result = $onResolved($this->exception, null);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
Promise\rethrow($result);
}
} catch (\Throwable $exception) {
Loop::defer(static function () use ($exception) {
throw $exception;
});
}
}
}