PHPackages                             jefyokta/php-promise - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jefyokta/php-promise

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jefyokta/php-promise
====================

Asyncronous Programming with Promise spec using swoole coroutine

v0.0.2(1mo ago)11MITPHPPHP ^8.2CI passing

Since May 2Pushed 1mo agoCompare

[ Source](https://github.com/jefyokta/php-promise)[ Packagist](https://packagist.org/packages/jefyokta/php-promise)[ RSS](/packages/jefyokta-php-promise/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

PHP Promise
===========

[](#php-promise)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1928d3dbe4cddabd2435133c9eb03b67de003a49d16874d28ea2223ed5b6ceca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6566796f6b74612f7068702d70726f6d6973652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jefyokta/php-promise)[![Tests](https://camo.githubusercontent.com/a7f63277d02a69d22893dbfffe2c07af660829ec52246256826945b89ef1c8b9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a6566796f6b74612f7068702d70726f6d6973652f74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/jefyokta/php-promise/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/62117bc48966eb53e599183220d16974902d6bd051aae5ca80dc98c6c4120376/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6566796f6b74612f7068702d70726f6d6973652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jefyokta/php-promise)

A PHP Promise implementation following the Promises/A+ specification, built on top of Swoole coroutines for true asynchronous programming.

Features
--------

[](#features)

- ✅ Promises/A+ specification compliant
- ✅ Built on Swoole coroutines for high-performance async operations
- ✅ Full promise chaining with `then()`, `catch()`, and `finally()`
- ✅ Static methods: `Promise::all()`, `Promise::race()`, `Promise::any()`
- ✅ Async/await syntax support with `async()` and `await()` functions
- ✅ Type-safe with PHP 8.2+ features
- ✅ Comprehensive test suite

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Swoole extension (recommended 5.0+)

Installation
------------

[](#installation)

Install via Composer:

```
composer require jefyokta/php-promise
```

Basic Usage
-----------

[](#basic-usage)

### Creating and Resolving Promises

[](#creating-and-resolving-promises)

```
use JefyOkta\PhpPromise\Promise;

$promise = new Promise(function ($resolve, $reject) {
    // Do some async work
    $resolve('Success!');
});

$result = $promise->wait(); // 'Success!'
```

### Promise Chaining

[](#promise-chaining)

```
Promise::resolve(2)
    ->then(fn($x) => $x * 3)
    ->then(fn($x) => $x + 1)
    ->then(fn($x) => "Result: $x")
    ->wait(); // 'Result: 7'
```

### Error Handling

[](#error-handling)

```
Promise::resolve(10)
    ->then(fn($x) => $x / 0) // This will throw DivisionByZeroError
    ->catch(fn($error) => "Caught: " . $error->getMessage())
    ->wait(); // 'Caught: Division by zero'
```

### Async/Await Syntax

[](#asyncawait-syntax)

```
use function JefyOkta\PhpPromise\async;
use function JefyOkta\PhpPromise\await;

async(function () {
    $result1 = await(async(fn() => 42)());
    $result2 = await(async(fn() => 58)());

    return $result1 + $result2;
})()->wait(); // 100
```

### Promise.all()

[](#promiseall)

```
$promises = [
    async(fn() => 1)(),
    async(fn() => 2)(),
    async(fn() => 3)(),
];

$result = Promise::all($promises)->wait(); // [1, 2, 3]
```

### Promise.race()

[](#promiserace)

```
$promises = [
    async(function () {
        Swoole\Coroutine::sleep(0.1);
        return 'slow';
    })(),
    async(function () {
        Swoole\Coroutine::sleep(0.05);
        return 'fast';
    })(),
];

$result = Promise::race($promises)->wait(); // 'fast'
```

### Promise.any()

[](#promiseany)

```
$promises = [
    Promise::reject('error1'),
    Promise::resolve('success'),
    Promise::reject('error2'),
];

$result = Promise::any($promises)->wait(); // 'success'
```

API Reference
-------------

[](#api-reference)

### Promise Class

[](#promise-class)

#### Constructor

[](#constructor)

```
new Promise(?callable $executor = null)
```

Creates a new Promise. The executor function receives `resolve` and `reject` callbacks.

#### Instance Methods

[](#instance-methods)

- `then(?callable $onFulfilled, ?callable $onRejected): Promise` - Chains fulfillment/rejection handlers
- `catch(callable $onRejected): Promise` - Chains rejection handler
- `finally(callable $onFinally): Promise` - Chains cleanup handler
- `wait(): mixed` - Blocks until promise settles and returns value

#### Static Methods

[](#static-methods)

- `Promise::resolve(mixed $value): Promise` - Creates a resolved promise
- `Promise::reject(mixed $reason): Promise` - Creates a rejected promise
- `Promise::all(array $promises): Promise` - Waits for all promises to resolve
- `Promise::race(array $promises): Promise` - Resolves/rejects with first settled promise
- `Promise::any(array $promises): Promise` - Resolves with first fulfilled promise

### Functions

[](#functions)

#### async(callable $fn): Asynchronous

[](#asynccallable-fn-asynchronous)

```
$asyncFn = async(fn($x) => $x * 2);
$promise = $asyncFn(5); // Promise that resolves to 10
```

#### await(Promise $promise): mixed

[](#awaitpromise-promise-mixed)

```
$result = await($promise); // Wait for promise resolution
```

Swoole Integration
------------------

[](#swoole-integration)

This library leverages Swoole coroutines for true asynchronous execution. Make sure Swoole is installed and the coroutine environment is available.

```
// Inside a coroutine context
Swoole\Coroutine\run(function () {
    $promise = async(function () {
        Swoole\Coroutine::sleep(1);
        return 'done';
    })();

    $result = await($promise);
    echo $result; // 'done' after 1 second
});
```

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request. Make sure to:

1. Add tests for new features
2. Follow PSR-12 coding standards
3. Update documentation as needed

License
-------

[](#license)

This project is licensed under the MIT License - see the LICENSE file for details.

Credits
-------

[](#credits)

- [Jepi Oktamipa](https://github.com/jefyokta) - Creator
- Built with [Swoole](https://www.swoole.com/) coroutines
- Inspired by JavaScript Promises/A+ specification

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance92

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

38d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/148698888?v=4)[Jepi Okta Mipa](/maintainers/jefyokta)[@jefyokta](https://github.com/jefyokta)

---

Top Contributors

[![jefyokta](https://avatars.githubusercontent.com/u/148698888?v=4)](https://github.com/jefyokta "jefyokta (16 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/jefyokta-php-promise/health.svg)

```
[![Health](https://phpackages.com/badges/jefyokta-php-promise/health.svg)](https://phpackages.com/packages/jefyokta-php-promise)
```

###  Alternatives

[anthonybudd/wp_route

1041.9k](/packages/anthonybudd-wp-route)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
