92 function then(callable $onFulfilled = null, callable $onRejected = null) {
97 $subPromise =
new self();
99 switch ($this->state) {
103 $this->subscribers[] = [$subPromise, $onFulfilled, $onRejected];
105 case self::FULFILLED :
110 case self::REJECTED :
131 return $this->
then(null, $onRejected);
142 if ($this->state !== self::PENDING) {
145 $this->state = self::FULFILLED;
147 foreach ($this->subscribers as $subscriber) {
162 if ($this->state !== self::PENDING) {
165 $this->state = self::REJECTED;
166 $this->value = $reason;
167 foreach ($this->subscribers as $subscriber) {
190 while ($this->state === self::PENDING) {
193 throw new \LogicException(
'There were no more events in the loop. This promise will never be fulfilled.');
202 if ($this->state === self::FULFILLED) {
211 } elseif (is_scalar($reason)) {
212 throw new Exception($reason);
214 $type = is_object($reason) ? get_class($reason) : gettype($reason);
215 throw new Exception(
'Promise was rejected with reason of type: ' .
$type);
260 if (is_callable($callBack)) {
263 $result = $callBack($this->value);
269 $result->then([$subPromise,
'fulfill'], [$subPromise,
'reject']);
281 if ($this->state === self::FULFILLED) {
282 $subPromise->
fulfill($this->value);
284 $subPromise->
reject($this->value);
299 function error(callable $onRejected) {
314 static function all(array $promises) {
otherwise(callable $onRejected)
Add a callback for when this promise is rejected.
An implementation of the Promise pattern.
then(callable $onFulfilled=null, callable $onRejected=null)
This method allows you to specify the callback that will be called after the promise has been fulfill...
static all(array $promises)
Deprecated.
fulfill($value=null)
Marks this promise as fulfilled and sets its return value.
invokeCallback(Promise $subPromise, callable $callBack=null)
This method is used to call either an onFulfilled or onRejected callback.
reject($reason=null)
Marks this promise as rejected, and set it's rejection reason.
This exception is thrown when the user tried to reject or fulfill a promise, after either of these ac...
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
const REJECTED
The asynchronous operation has completed with an error.
wait()
Stops execution until this promise is resolved.
tick($block=false)
Executes all pending events.
error(callable $onRejected)
Alias for 'otherwise'.
const FULFILLED
The asynchronous operation has completed, and has a result.
__construct(callable $executor=null)
Creates the promise.
const PENDING
The asynchronous operation is pending.