Promise
in package
implements
PromiseInterface
Promises/A+ implementation that avoids recursion when possible.
Tags
Table of Contents
Interfaces
- PromiseInterface
- A promise represents the eventual result of an asynchronous operation.
Properties
- $cancelFn : mixed
- $handlers : mixed
- $result : mixed
- $state : mixed
- $waitFn : mixed
- $waitList : mixed
Methods
- __construct() : mixed
- cancel() : void
- Cancels the promise if possible.
- getState() : string
- Get the state of the promise ("pending", "rejected", or "fulfilled").
- otherwise() : PromiseInterface
- Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.
- reject() : void
- Reject the promise with the given reason.
- resolve() : void
- Resolve the promise with the given value.
- then() : PromiseInterface
- Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.
- wait() : mixed
- Waits until the promise completes if possible.
- callHandler() : void
- Call a stack of handlers using a specific callback index and value.
- invokeWaitFn() : void
- invokeWaitList() : void
- settle() : void
- waitIfPending() : void
Properties
$cancelFn
private
mixed
$cancelFn
$handlers
private
mixed
$handlers
= []
$result
private
mixed
$result
$state
private
mixed
$state
= self::PENDING
$waitFn
private
mixed
$waitFn
$waitList
private
mixed
$waitList
Methods
__construct()
public
__construct([callable $waitFn = null ][, callable $cancelFn = null ]) : mixed
Parameters
- $waitFn : callable = null
-
Fn that when invoked resolves the promise.
- $cancelFn : callable = null
-
Fn that when invoked cancels the promise.
cancel()
Cancels the promise if possible.
public
cancel() : void
getState()
Get the state of the promise ("pending", "rejected", or "fulfilled").
public
getState() : string
The three states can be checked against the constants defined on PromiseInterface: PENDING, FULFILLED, and REJECTED.
Return values
stringotherwise()
Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.
public
otherwise(callable $onRejected) : PromiseInterface
Parameters
- $onRejected : callable
-
Invoked when the promise is rejected.
Return values
PromiseInterfacereject()
Reject the promise with the given reason.
public
reject(mixed $reason) : void
Parameters
- $reason : mixed
resolve()
Resolve the promise with the given value.
public
resolve(mixed $value) : void
Parameters
- $value : mixed
then()
Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.
public
then([callable|null $onFulfilled = null ][, callable|null $onRejected = null ]) : PromiseInterface
Parameters
- $onFulfilled : callable|null = null
-
Invoked when the promise fulfills.
- $onRejected : callable|null = null
-
Invoked when the promise is rejected.
Return values
PromiseInterfacewait()
Waits until the promise completes if possible.
public
wait([bool $unwrap = true ]) : mixed
Pass $unwrap as true to unwrap the result of the promise, either returning the resolved value or throwing the rejected exception.
If the promise cannot be waited on, then the promise will be rejected.
Parameters
- $unwrap : bool = true
callHandler()
Call a stack of handlers using a specific callback index and value.
private
static callHandler(int $index, mixed $value, array<string|int, mixed> $handler) : void
Parameters
- $index : int
-
1 (resolve) or 2 (reject).
- $value : mixed
-
Value to pass to the callback.
- $handler : array<string|int, mixed>
-
Array of handler data (promise and callbacks).
invokeWaitFn()
private
invokeWaitFn() : void
invokeWaitList()
private
invokeWaitList() : void
settle()
private
settle(string $state, mixed $value) : void
Parameters
- $state : string
- $value : mixed
waitIfPending()
private
waitIfPending() : void