mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Rename and move Promise
This commit is contained in:
parent
de3ada9014
commit
b572f9be40
@ -39,7 +39,7 @@ try {
|
|||||||
private $fail;
|
private $fail;
|
||||||
|
|
||||||
public function __construct() {
|
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->resolve = $resolve;
|
||||||
$this->fail = $fail;
|
$this->fail = $fail;
|
||||||
});
|
});
|
||||||
|
@ -1,33 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Amp\Awaitable;
|
namespace Amp\Awaitable\Internal;
|
||||||
|
|
||||||
use Interop\Async\Awaitable;
|
use Interop\Async\Awaitable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Promise is an awaitable that provides the functions to resolve or fail the promise to the resolver function
|
* An awaitable that cannot be externally resolved. Used by Deferred in development mode.
|
||||||
* given to the constructor. A Promise cannot be externally resolved. Only the functions provided to the constructor
|
|
||||||
* may resolve the Promise.
|
|
||||||
*/
|
*/
|
||||||
final class Promise implements Awaitable {
|
final class PrivateAwaitable implements Awaitable {
|
||||||
use Internal\Placeholder;
|
use Placeholder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param callable(callable $resolve, callable $reject): void $resolver
|
* @param callable(callable $resolve, callable $reject): void $resolver
|
||||||
*/
|
*/
|
||||||
public function __construct(callable $resolver) {
|
public function __construct(callable $resolver) {
|
||||||
/**
|
/**
|
||||||
* Resolves the promise with the given promise or value. If another promise, this promise takes
|
* Resolves the awaitable with the given awaitable or value.
|
||||||
* on the state of that promise. If a value, the promise will be fulfilled with that value.
|
|
||||||
*
|
*
|
||||||
* @param mixed $value A promise can be resolved with anything other than itself.
|
* @param mixed $value
|
||||||
*/
|
*/
|
||||||
$resolve = function ($value = null) {
|
$resolve = function ($value = null) {
|
||||||
$this->resolve($value);
|
$this->resolve($value);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fails the promise with the given exception.
|
* Fails the awaitable with the given exception.
|
||||||
*
|
*
|
||||||
* @param \Exception $reason
|
* @param \Exception $reason
|
||||||
*/
|
*/
|
@ -201,9 +201,11 @@ function adapt($thenable) {
|
|||||||
return new Failure(new \InvalidArgumentException("Must provide an object with a then() method"));
|
return new Failure(new \InvalidArgumentException("Must provide an object with a then() method"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise(function (callable $resolve, callable $fail) use ($thenable) {
|
$deferred = new Deferred;
|
||||||
$thenable->then($resolve, $fail);
|
|
||||||
});
|
$thenable->then([$deferred, 'resolve'], [$deferred, 'fail']);
|
||||||
|
|
||||||
|
return $deferred->getAwaitable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user