1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/lib/Failure.php

48 lines
1.0 KiB
PHP
Raw Normal View History

<?php
2016-08-15 23:46:26 -05:00
2016-05-23 22:48:28 -05:00
namespace Amp;
2016-05-21 09:44:52 -05:00
use React\Promise\PromiseInterface as ReactPromise;
2016-06-01 12:18:11 -05:00
/**
* Creates a failed promise using the given exception.
*
2020-03-28 20:27:42 +01:00
* @template-covariant TValue
* @template-implements Promise<TValue>
2016-06-01 12:18:11 -05:00
*/
2018-06-18 20:00:01 +02:00
final class Failure implements Promise
{
2020-09-24 11:52:22 -05:00
private \Throwable $exception;
2016-05-21 09:44:52 -05:00
/**
2016-08-11 14:35:58 -05:00
* @param \Throwable $exception Rejection reason.
2016-05-21 09:44:52 -05:00
*/
2018-06-18 20:00:01 +02:00
public function __construct(\Throwable $exception)
{
2016-05-21 09:44:52 -05:00
$this->exception = $exception;
}
/**
* {@inheritdoc}
*/
2020-09-24 11:52:22 -05:00
public function onResolve(callable $onResolved): void
2018-06-18 20:00:01 +02:00
{
2020-09-24 11:52:22 -05:00
Loop::defer(function () use ($onResolved): void {
/** @var mixed $result */
$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);
}
2020-09-24 11:52:22 -05:00
});
2016-05-21 09:44:52 -05:00
}
}