2016-05-21 09:44:52 -05:00
|
|
|
<?php
|
|
|
|
|
2016-05-23 22:48:28 -05:00
|
|
|
namespace Amp;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
|
|
|
use Interop\Async\Awaitable;
|
2016-08-11 14:35:58 -05:00
|
|
|
use Interop\Async\Loop;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2016-06-01 12:18:11 -05:00
|
|
|
/**
|
|
|
|
* Creates a failed awaitable using the given exception.
|
|
|
|
*/
|
|
|
|
final class Failure implements Awaitable {
|
2016-05-21 09:44:52 -05:00
|
|
|
/**
|
2016-08-11 14:35:58 -05:00
|
|
|
* @var \Throwable $exception
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
|
|
|
private $exception;
|
|
|
|
|
|
|
|
/**
|
2016-08-11 14:35:58 -05:00
|
|
|
* @param \Throwable $exception Rejection reason.
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
2016-08-11 14:35:58 -05:00
|
|
|
public function __construct(\Throwable $exception) {
|
2016-05-21 09:44:52 -05:00
|
|
|
$this->exception = $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function when(callable $onResolved) {
|
|
|
|
try {
|
|
|
|
$onResolved($this->exception, null);
|
|
|
|
} catch (\Throwable $exception) {
|
2016-05-22 13:43:37 -05:00
|
|
|
Loop::defer(static function () use ($exception) {
|
2016-05-21 09:44:52 -05:00
|
|
|
throw $exception;
|
2016-05-22 13:43:37 -05:00
|
|
|
});
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 13:43:37 -05:00
|
|
|
}
|