1
0
mirror of https://github.com/danog/amp.git synced 2025-01-23 05:41:25 +01:00
amp/lib/Success.php

49 lines
1.2 KiB
PHP
Raw Normal View History

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\Loop;
use Interop\Async\Awaitable;
2016-06-01 12:18:11 -05:00
/**
* Creates a successful awaitable using the given value (which can be any value except another object implementing
* \Interop\Async\Awaitable).
*/
final class Success implements Awaitable {
2016-05-21 09:44:52 -05:00
/**
* @var mixed
*/
private $value;
/**
* @param mixed $value Anything other than an Awaitable object.
*
* @throws \InvalidArgumentException If an awaitable is given as the value.
*/
2016-05-29 10:40:00 -05:00
public function __construct($value = null)
2016-05-21 09:44:52 -05:00
{
if ($value instanceof Awaitable) {
2016-05-21 12:12:55 -05:00
throw new \InvalidArgumentException("Cannot use an awaitable as success value");
2016-05-21 09:44:52 -05:00
}
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function when(callable $onResolved) {
try {
$onResolved(null, $this->value);
} catch (\Throwable $exception) {
Loop::defer(static function () use ($exception) {
2016-05-21 09:44:52 -05:00
throw $exception;
});
2016-05-21 09:44:52 -05:00
} catch (\Exception $exception) {
Loop::defer(static function () use ($exception) {
2016-05-21 09:44:52 -05:00
throw $exception;
});
2016-05-21 09:44:52 -05:00
}
}
}