ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sabre\Event\Promise Class Reference

An implementation of the Promise pattern. More...

+ Collaboration diagram for Sabre\Event\Promise:

Data Structures

class  FunctionsTest
 
class  PromiseTest
 

Public Member Functions

 __construct (callable $executor=null)
 Creates the promise. More...
 
 then (callable $onFulfilled=null, callable $onRejected=null)
 This method allows you to specify the callback that will be called after the promise has been fulfilled or rejected. More...
 
 otherwise (callable $onRejected)
 Add a callback for when this promise is rejected. More...
 
 fulfill ($value=null)
 Marks this promise as fulfilled and sets its return value. More...
 
 reject ($reason=null)
 Marks this promise as rejected, and set it's rejection reason. More...
 
 wait ()
 Stops execution until this promise is resolved. More...
 
 error (callable $onRejected)
 Alias for 'otherwise'. More...
 

Static Public Member Functions

static all (array $promises)
 Deprecated. More...
 

Data Fields

const PENDING = 0
 The asynchronous operation is pending. More...
 
const FULFILLED = 1
 The asynchronous operation has completed, and has a result. More...
 
const REJECTED = 2
 The asynchronous operation has completed with an error. More...
 
 $state = self::PENDING
 

Protected Attributes

 $subscribers = []
 
 $value = null
 

Private Member Functions

 invokeCallback (Promise $subPromise, callable $callBack=null)
 This method is used to call either an onFulfilled or onRejected callback. More...
 

Detailed Description

An implementation of the Promise pattern.

A promise represents the result of an asynchronous operation. At any given point a promise can be in one of three states:

  1. Pending (the promise does not have a result yet).
  2. Fulfilled (the asynchronous operation has completed with a result).
  3. Rejected (the asynchronous operation has completed with an error).

To get a callback when the operation has finished, use the then method.

Author
Evert Pot (http://evertpot.com/) http://sabre.io/license/ Modified BSD License

Definition at line 23 of file Promise.php.

Constructor & Destructor Documentation

◆ __construct()

Sabre\Event\Promise::__construct ( callable  $executor = null)

Creates the promise.

The passed argument is the executor. The executor is automatically called with two arguments.

Each are callbacks that map to $this->fulfill and $this->reject. Using the executor is optional.

Parameters
callable$executor

Definition at line 58 of file Promise.php.

58  {
59 
60  if ($executor) {
61  $executor(
62  [$this, 'fulfill'],
63  [$this, 'reject']
64  );
65  }
66 
67  }

Member Function Documentation

◆ all()

◆ error()

Sabre\Event\Promise::error ( callable  $onRejected)

Alias for 'otherwise'.

This function is now deprecated and will be removed in a future version.

Parameters
callable$onRejected
Deprecated:
Returns
Promise

Definition at line 299 of file Promise.php.

References Sabre\Event\Promise\otherwise().

Referenced by Sabre\Event\Promise\all().

299  {
300 
301  return $this->otherwise($onRejected);
302 
303  }
otherwise(callable $onRejected)
Add a callback for when this promise is rejected.
Definition: Promise.php:129
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fulfill()

Sabre\Event\Promise::fulfill (   $value = null)

Marks this promise as fulfilled and sets its return value.

Parameters
mixed$value
Returns
void

Definition at line 141 of file Promise.php.

References Sabre\Event\Promise\$value, and Sabre\Event\Promise\invokeCallback().

Referenced by Sabre\Event\Promise\invokeCallback().

141  {
142  if ($this->state !== self::PENDING) {
143  throw new PromiseAlreadyResolvedException('This promise is already resolved, and you\'re not allowed to resolve a promise more than once');
144  }
145  $this->state = self::FULFILLED;
146  $this->value = $value;
147  foreach ($this->subscribers as $subscriber) {
148  $this->invokeCallback($subscriber[0], $subscriber[1]);
149  }
150  }
invokeCallback(Promise $subPromise, callable $callBack=null)
This method is used to call either an onFulfilled or onRejected callback.
Definition: Promise.php:252
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ invokeCallback()

Sabre\Event\Promise::invokeCallback ( Promise  $subPromise,
callable  $callBack = null 
)
private

This method is used to call either an onFulfilled or onRejected callback.

This method makes sure that the result of these callbacks are handled correctly, and any chained promises are also correctly fulfilled or rejected.

Parameters
Promise$subPromise
callable$callBack
Returns
void

Definition at line 252 of file Promise.php.

References $result, Sabre\Event\Promise\fulfill(), Sabre\Event\Loop\nextTick(), and Sabre\Event\Promise\reject().

Referenced by Sabre\Event\Promise\fulfill(), Sabre\Event\Promise\reject(), and Sabre\Event\Promise\then().

252  {
253 
254  // We use 'nextTick' to ensure that the event handlers are always
255  // triggered outside of the calling stack in which they were originally
256  // passed to 'then'.
257  //
258  // This makes the order of execution more predictable.
259  Loop\nextTick(function() use ($callBack, $subPromise) {
260  if (is_callable($callBack)) {
261  try {
262 
263  $result = $callBack($this->value);
264  if ($result instanceof self) {
265  // If the callback (onRejected or onFulfilled)
266  // returned a promise, we only fulfill or reject the
267  // chained promise once that promise has also been
268  // resolved.
269  $result->then([$subPromise, 'fulfill'], [$subPromise, 'reject']);
270  } else {
271  // If the callback returned any other value, we
272  // immediately fulfill the chained promise.
273  $subPromise->fulfill($result);
274  }
275  } catch (Exception $e) {
276  // If the event handler threw an exception, we need to make sure that
277  // the chained promise is rejected as well.
278  $subPromise->reject($e);
279  }
280  } else {
281  if ($this->state === self::FULFILLED) {
282  $subPromise->fulfill($this->value);
283  } else {
284  $subPromise->reject($this->value);
285  }
286  }
287  });
288  }
$result
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
Definition: functions.php:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ otherwise()

Sabre\Event\Promise::otherwise ( callable  $onRejected)

Add a callback for when this promise is rejected.

Its usage is identical to then(). However, the otherwise() function is preferred.

Parameters
callable$onRejected
Returns
Promise

Definition at line 129 of file Promise.php.

References Sabre\Event\Promise\then().

Referenced by Sabre\Event\Promise\error().

129  {
130 
131  return $this->then(null, $onRejected);
132 
133  }
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...
Definition: Promise.php:92
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reject()

Sabre\Event\Promise::reject (   $reason = null)

Marks this promise as rejected, and set it's rejection reason.

While it's possible to use any PHP value as the reason, it's highly recommended to use an Exception for this.

Parameters
mixed$reason
Returns
void

Definition at line 161 of file Promise.php.

References Sabre\Event\Promise\invokeCallback().

Referenced by Sabre\Event\Promise\invokeCallback(), and Sabre\Event\Promise\FunctionsTest\testReject().

161  {
162  if ($this->state !== self::PENDING) {
163  throw new PromiseAlreadyResolvedException('This promise is already resolved, and you\'re not allowed to resolve a promise more than once');
164  }
165  $this->state = self::REJECTED;
166  $this->value = $reason;
167  foreach ($this->subscribers as $subscriber) {
168  $this->invokeCallback($subscriber[0], $subscriber[2]);
169  }
170 
171  }
invokeCallback(Promise $subPromise, callable $callBack=null)
This method is used to call either an onFulfilled or onRejected callback.
Definition: Promise.php:252
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ then()

Sabre\Event\Promise::then ( callable  $onFulfilled = null,
callable  $onRejected = null 
)

This method allows you to specify the callback that will be called after the promise has been fulfilled or rejected.

Both arguments are optional.

This method returns a new promise, which can be used for chaining. If either the onFulfilled or onRejected callback is called, you may return a result from this callback.

If the result of this callback is yet another promise, the result of that promise will be used to set the result of the returned promise.

If either of the callbacks return any other value, the returned promise is automatically fulfilled with that value.

If either of the callbacks throw an exception, the returned promise will be rejected and the exception will be passed back.

Parameters
callable$onFulfilled
callable$onRejected
Returns
Promise

Definition at line 92 of file Promise.php.

References Sabre\Event\Promise\invokeCallback().

Referenced by Sabre\Event\Promise\otherwise(), Sabre\Event\Promise\PromiseTest\testChain(), Sabre\Event\Promise\PromiseTest\testChainPromise(), and Sabre\Event\Promise\PromiseTest\testFromFailureHandler().

92  {
93 
94  // This new subPromise will be returned from this function, and will
95  // be fulfilled with the result of the onFulfilled or onRejected event
96  // handlers.
97  $subPromise = new self();
98 
99  switch ($this->state) {
100  case self::PENDING :
101  // The operation is pending, so we keep a reference to the
102  // event handlers so we can call them later.
103  $this->subscribers[] = [$subPromise, $onFulfilled, $onRejected];
104  break;
105  case self::FULFILLED :
106  // The async operation is already fulfilled, so we trigger the
107  // onFulfilled callback asap.
108  $this->invokeCallback($subPromise, $onFulfilled);
109  break;
110  case self::REJECTED :
111  // The async operation failed, so we call teh onRejected
112  // callback asap.
113  $this->invokeCallback($subPromise, $onRejected);
114  break;
115  }
116  return $subPromise;
117 
118  }
invokeCallback(Promise $subPromise, callable $callBack=null)
This method is used to call either an onFulfilled or onRejected callback.
Definition: Promise.php:252
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ wait()

Sabre\Event\Promise::wait ( )

Stops execution until this promise is resolved.

This method stops exection completely. If the promise is successful with a value, this method will return this value. If the promise was rejected, this method will throw an exception.

This effectively turns the asynchronous operation into a synchronous one. In PHP it might be useful to call this on the last promise in a chain.

Exceptions
Exception
Returns
mixed

Definition at line 187 of file Promise.php.

References $type, Sabre\Event\Promise\$value, and Sabre\Event\Loop\tick().

187  {
188 
189  $hasEvents = true;
190  while ($this->state === self::PENDING) {
191 
192  if (!$hasEvents) {
193  throw new \LogicException('There were no more events in the loop. This promise will never be fulfilled.');
194  }
195 
196  // As long as the promise is not fulfilled, we tell the event loop
197  // to handle events, and to block.
198  $hasEvents = Loop\tick(true);
199 
200  }
201 
202  if ($this->state === self::FULFILLED) {
203  // If the state of this promise is fulfilled, we can return the value.
204  return $this->value;
205  } else {
206  // If we got here, it means that the asynchronous operation
207  // errored. Therefore we need to throw an exception.
208  $reason = $this->value;
209  if ($reason instanceof Exception) {
210  throw $reason;
211  } elseif (is_scalar($reason)) {
212  throw new Exception($reason);
213  } else {
214  $type = is_object($reason) ? get_class($reason) : gettype($reason);
215  throw new Exception('Promise was rejected with reason of type: ' . $type);
216  }
217  }
218 
219 
220  }
$type
tick($block=false)
Executes all pending events.
Definition: functions.php:151
+ Here is the call graph for this function:

Field Documentation

◆ $state

Sabre\Event\Promise::$state = self::PENDING

Definition at line 45 of file Promise.php.

◆ $subscribers

Sabre\Event\Promise::$subscribers = []
protected

Definition at line 229 of file Promise.php.

◆ $value

◆ FULFILLED

const Sabre\Event\Promise::FULFILLED = 1

The asynchronous operation has completed, and has a result.

Definition at line 33 of file Promise.php.

◆ PENDING

const Sabre\Event\Promise::PENDING = 0

The asynchronous operation is pending.

Definition at line 28 of file Promise.php.

Referenced by Sabre\Event\coroutine().

◆ REJECTED

const Sabre\Event\Promise::REJECTED = 2

The asynchronous operation has completed with an error.

Definition at line 38 of file Promise.php.


The documentation for this class was generated from the following file: