1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00

Rename and move Promise

This commit is contained in:
Aaron Piotrowski 2016-05-22 13:24:39 -05:00
parent de3ada9014
commit b572f9be40
3 changed files with 13 additions and 14 deletions

View File

@ -39,7 +39,7 @@ try {
private $fail;
public function __construct() {
$this->awaitable = new Promise(function (callable $resolve, callable $fail) {
$this->awaitable = new Internal\PrivateAwaitable(function (callable $resolve, callable $fail) {
$this->resolve = $resolve;
$this->fail = $fail;
});

View File

@ -1,33 +1,30 @@
<?php
namespace Amp\Awaitable;
namespace Amp\Awaitable\Internal;
use Interop\Async\Awaitable;
/**
* A Promise is an awaitable that provides the functions to resolve or fail the promise to the resolver function
* given to the constructor. A Promise cannot be externally resolved. Only the functions provided to the constructor
* may resolve the Promise.
* An awaitable that cannot be externally resolved. Used by Deferred in development mode.
*/
final class Promise implements Awaitable {
use Internal\Placeholder;
final class PrivateAwaitable implements Awaitable {
use Placeholder;
/**
* @param callable(callable $resolve, callable $reject): void $resolver
*/
public function __construct(callable $resolver) {
/**
* Resolves the promise with the given promise or value. If another promise, this promise takes
* on the state of that promise. If a value, the promise will be fulfilled with that value.
* Resolves the awaitable with the given awaitable or value.
*
* @param mixed $value A promise can be resolved with anything other than itself.
* @param mixed $value
*/
$resolve = function ($value = null) {
$this->resolve($value);
};
/**
* Fails the promise with the given exception.
* Fails the awaitable with the given exception.
*
* @param \Exception $reason
*/

View File

@ -201,9 +201,11 @@ function adapt($thenable) {
return new Failure(new \InvalidArgumentException("Must provide an object with a then() method"));
}
return new Promise(function (callable $resolve, callable $fail) use ($thenable) {
$thenable->then($resolve, $fail);
});
$deferred = new Deferred;
$thenable->then([$deferred, 'resolve'], [$deferred, 'fail']);
return $deferred->getAwaitable();
}
/**