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

48 lines
1.1 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
/**
* Deferred is a container for a promise that is resolved using the resolve() and fail() methods of this object.
* The contained promise may be accessed using the promise() method. This object should not be part of a public
* API, but used internally to create and resolve a promise.
*/
final class Deferred {
/** @var \Amp\Promise */
private $promise;
public function __construct() {
$this->promise = new class implements Promise {
use Internal\Placeholder {
resolve as public;
fail as public;
2016-05-21 09:44:52 -05:00
}
};
}
2016-05-21 09:44:52 -05:00
/**
* @return \Amp\Promise
*/
public function promise(): Promise {
return $this->promise;
}
2016-05-21 09:44:52 -05:00
/**
* Fulfill the promise with the given value.
*
* @param mixed $value
*/
public function resolve($value = null) {
$this->promise->resolve($value);
}
2016-05-21 09:44:52 -05:00
/**
* Fails the promise the the given reason.
*
* @param \Throwable $reason
*/
public function fail(\Throwable $reason) {
$this->promise->fail($reason);
2016-05-21 09:44:52 -05:00
}
}