fyre/promise

An promise library.

v2.0.6 2024-06-25 04:05 UTC

This package is auto-updated.

Last update: 2024-06-25 04:05:48 UTC


README

FyrePromise

FyrePromise is a free, opens-source promise library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/promise

In PHP:

use Fyre\Promise\Promise;

Promise Creation

  • $callback is a Closure.
  • $sync is a boolean indicating whether to execute the Promise synchronously, and will default to false.
$promise = new Promise($callback, $sync);

The $callback should be expressed in the following format:

$callback = function(Closure $resolve, Closure $reject): void {
    $resolve();
};

Methods

Catch

Execute a callback if the Promise is rejected.

  • $onRejected is a Closure that will execute when the Promise is rejected.
$promise->catch($onRejected);

Finally

Execute a callback when the Promise is settled.

  • $onFinally is a Closure that will execute when the Promise has settled.
$promise->finally($onFinally);

Get Rejected Reason

Get the rejected reason.

$rejectedReason = $promise->getRejectedReason();

Get Resolved Value

Get the resolved value.

$resolvedValue = $promise->getResolvedValue();

Is Rejected

Determine whether the Promise was rejected.

$isRejected = $promise->isRejected();

Is Resolved

Determine whether the Promise has resolved.

$isResolved = $promise->isResolved();

Is Settled

Determine whether the Promise has settled.

$isSettled = $promise->isSettled();

Poll

Poll the child process to determine if the Promise has settled.

$isSettled = $promise->poll();

Then

Execute a callback when the Promise is resolved.

  • $onFulfilled is a Closure that will execute when the Promise is resolved.
  • $onRejected is a Closure that will execute when the Promise is rejected, and will default to null.
$promise->then($onFulfilled, $onRejected);

Wait

Wait for the Promise to settle.

$promise->wait();

Static Methods

All

Wait for all promises to settle.

  • $promises is an array containing the promises to wait for.
Promise::all($promises);

Await

Wait for a Promise to settle.

  • $promise is the Promise to wait for.
$resolvedValue = Promise::await($promise);

Reject

Create a Promise that rejects.

  • $reason is a string representing the rejected reason, and will default to null.
$promise = Promise::reject($reason);

Resolve

Create a Promise that resolves.

  • $value is the resolved value, and will default to null.
$promise = Promise::resolve($value);