Skip to content

A lightweight and concise implementation of Promises in PHP

Notifications You must be signed in to change notification settings

ucscode/promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Promise

Promise is a simple and lightweight implementation of Javascript Promises in PHP. Promises provide a clean and structured way to work with asynchronous operations, allowing you to handle deferred execution and manage asynchronous workflows more effectively.

Features

  • Promises/A+ Compliant: Follows the Promises/A+ specification for consistent behavior.
  • Chaining: Chain promises together using the then method.
  • Error Handling: Handle errors using the catch method.
  • Finalization: Execute code regardless of the promise's state with the finally method.

Getting Started

Installation

composer require ucscode/promise

Usage

use Ucscode\Promise\Promise;

$promise = new Promise(function ($resolve, $reject) {
    // Asynchronous operation
    sleep(20);
    $resolve("Operation Successful");
});

$promise->then(
    fn ($value) => "Fulfilled: $value",
    fn ($reason) => "Rejected: $reason"
)
->finally(fn () => "Operation complete");

Multiple Promises

The Promise::all method takes an array of promises, and collects their fulfilled values. If all promises are fulfilled, it resolves with an array of fulfilled values. If any promise is rejected, it rejects with the reason of the first rejected promise.

$promises = [
    new Promise(function ($resolve) { $resolve(1); }),
    new Promise(function ($resolve) { $resolve(2); }),
    new Promise(function ($resolve) { $resolve(3); }),
];

Promise::all($promises)->then(
    function ($values) {
        // All promises fulfilled
        var_dump($values); // Output: array(1, 2, 3)
    },
    fn ($reason) => "At least one promise rejected"
);

Contributing

Contributions are welcome! Feel free to open issues or pull requests.

License

This project is licensed under the MIT License

About

A lightweight and concise implementation of Promises in PHP

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages