PHPackages                             php-promise/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. php-promise/promise

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

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

PHP Promise

247871PHP

Since Apr 4Pushed 7y ago1 watchersCompare

[ Source](https://github.com/php-promise/promise)[ Packagist](https://packagist.org/packages/php-promise/promise)[ RSS](/packages/php-promise-promise/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#php-promise)

[![](https://user-images.githubusercontent.com/1282995/42281650-2887c028-7fdf-11e8-962c-bf7bdd339fdf.png)](https://user-images.githubusercontent.com/1282995/42281650-2887c028-7fdf-11e8-962c-bf7bdd339fdf.png)

The PHP Promise is a library that asynchronously processes PHP like JavaScript Promise. The library uses `pthreads`, which is a PHP extension. If you want to use the library, you need to install the `pthreads` extension. You may also need to re-compile PHP with `--enable-maincontainer-zts` option in configuration.

See also:

For example in Dockerfile:

```
FROM centos:7

# Setup
RUN yum -y install epel-release wget
RUN cd /tmp && wget http://jp2.php.net/get/php-7.2.7.tar.gz/from/this/mirror -O php-7.2 && tar zxvf php-7.2

# Dependencies installation
RUN yum -y install git gcc gcc-c++ make libxml2-devel libicu-devel openssl-devel

# PHP installation
RUN cd /tmp/php-7.2.7 && \
    ./configure --enable-maintainer-zts --enable-pcntl --enable-intl --enable-zip --enable-pdo --enable-sockets --with-openssl && \
    make && \
    make install

# pthreads installation
RUN yum -y install autoconf
RUN cd /tmp && git clone https://github.com/krakjoe/pthreads.git && cd pthreads && \
    phpize && \
    ./configure && \
    make && \
    make install

# Add an extension to php.ini
RUN echo extension=pthreads.so >> /usr/local/lib/php.ini
```

The PHP Promise structure
=========================

[](#the-php-promise-structure)

The PHP Promise structure is below. [![The Promise structure](https://user-images.githubusercontent.com/1282995/42298295-20c6456a-8040-11e8-9c66-8b3422d327c8.jpeg)](https://user-images.githubusercontent.com/1282995/42298295-20c6456a-8040-11e8-9c66-8b3422d327c8.jpeg)

Requirements
============

[](#requirements)

- PHP &gt;= 7.2
- pthreads 3

Get Started
===========

[](#get-started)

Run the composer require command.

```
$ composer require php-promise/promise:dev-master

```

Next, run the example code below and you will get started to Promise

```
// After 5 seconds, Promise will say a message "solved It!"
$promise = (new \Promise\Promise(function (Resolver $resolve, Rejecter $reject) {
    sleep(5);
    $resolve("Solved It!\n");
}))->then(function ($message) {
    echo $message;
});
```

You can use `Promise::all` which waits multiple Promise processing, which will collect Promise results.

```
// Say as follows:
// [RESOLVE] After 3 seconds says!
// [RESOLVE] After 5 seconds says!
// Sorry, Promise was rejected.
$promises = [];
$promises[] = (new \Promise\Promise(function (Resolver $resolve, Rejecter $reject) {
    sleep(5);
    $resolve("[RESOLVE] After 5 seconds!\n");
}))->then(function ($message) {
    echo $message;
});
$promises[] = (new \Promise\Promise(function (Resolver $resolve, Rejecter $reject) {
    sleep(3);
    $reject("[REJECT] After 3 seconds!\n");
}))->then(function ($message) {
    echo $message;
});

\Promise\Promise::all($promises)->then(function () {
    echo "Okay, Promise is glad\n";
})->catch(function () {
    echo "Sorry, Promise was rejected\n";
});

// or

\Promise\Promise::all($promises[0], $promises[1])->then(function () {
    echo "Okay, Promise is glad\n";
})->catch(function () {
    echo "Sorry, Promise was rejected\n";
});
```

Supported Frameworks
====================

[](#supported-frameworks)

- Laravel
    - Need to use SafetyLoader.
- CakePHP

Use Case
========

[](#use-case)

- For example, you can use it to run heavely processing.

Provide methods
===============

[](#provide-methods)

Promise::\_\_construct( callable $callee( Resolver $resolve, Rejecter $reject, ...$parameters ), ...$parameters ): Promise
--------------------------------------------------------------------------------------------------------------------------

[](#promise__construct-callable-callee-resolver-resolve-rejecter-reject-parameters--parameters--promise)

- The constructor is called from your code and immediately runs `$callee` function.
- `$callee` has two parameters that are `$resolve` and `$reject`, which are callable functions.
- `$resolve` is called in `$callee`, which immediately runs the resolved function in `Promise::then`.
- `$reject` is called in `$callee`, which immediately runs the rejected function in `Promise::catch`.
- You can define `$parameters` which you want to pass resource, object and any types to Promise context.

e.g.)

```
(new \Promise\Promise(function (Resolver $resolve, Rejecter $reject) {
    // Promise call `then` method when you called `$resolve` here.
    $resolve();

    // Promise call `reject` method when you called `$reject` here.
    $reject();
}))->then(function () {
    echo 'You can see this message when `$resolve` called.';
})->catch(function () {
    echo 'You can see this message when `$reject` called.';
});
```

e.g.)

```
$handle = fopen('test.log', 'rw');
(new \Promise\Promise(function (Resolver $resolve, Rejecter $reject, $handle) {
    $reject($handle);
}, $handle))->then(function ($handle) {
    fwrite($handle, 'You can pass resource parameter.');
});
```

Promise::all( Promise ...$promises ): Promise
---------------------------------------------

[](#promiseall-promise-promises--promise)

- Wait multiple promise processing, then which will collect to promise results.

Promise::race( Promise ...$promises ): Promise
----------------------------------------------

[](#promiserace-promise-promises--promise)

- `Promise::race` return a Promise when promise get a success or failed.

Promise::then( callable $onFulfilled, callable $rejected ): Promise
-------------------------------------------------------------------

[](#promisethen-callable-onfulfilled-callable-rejected--promise)

- `Promise::catch` is called when `$resolve` is called.

e.g.)

```
(new \Promise\Promise(function (Resolver $resolve, Rejecter $reject) {
    $resolve();
}))->then(function () {
    echo 'You can see this message when `$resolve` call.';
});
```

Promise::catch( callable $rejected ): Promise
---------------------------------------------

[](#promisecatch-callable-rejected--promise)

- `Promise::catch` is called when `$reject` is called.

e.g.)

```
(new \Promise\Promise(function (Resolver $resolve, Rejecter $reject) {
    $reject();
}))->catch(function () {
    echo 'You can see this message when `$resolve` call.';
});
```

Promise::finally( callable $onFinally ): Promise
------------------------------------------------

[](#promisefinally-callable-onfinally--promise)

- `Promise::finally` is called when `$resolve` or `$catch` is called.

e.g.)

```
(new \Promise\Promise(function (Resolver $resolve, Rejecter $reject) {
    $resolve();
}))->then(function () {
    // do something
})->catch(function () {
    // do something
})->finally(function () {
     echo 'You can see this message when `$resolve` or `$catch` called.';
});
```

Annotations
===========

[](#annotations)

- `resolve` and `reject` can pass only serializable values on PHP because of `pthreads` has problem when pass parameters to thread to thread.
- For example, cannot pass values `resource` which opened by stream as such as `fopen`.

Unit Tests
==========

[](#unit-tests)

PHPUnit Testing:

```
$ composer run-script phpunit

```

Code Sniffer Testing:

```
$ composer run-script phpcs

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community8

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/727125492e6e5417b2b80c6834e0c288f881e7a5daca295a6684c354e3bc87dc?d=identicon)[m3m0r7](/maintainers/m3m0r7)

---

Top Contributors

[![m3m0r7](https://avatars.githubusercontent.com/u/1282995?v=4)](https://github.com/m3m0r7 "m3m0r7 (23 commits)")

### Embed Badge

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

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

PHPackages © 2026

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