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

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

streamcommon/promise
====================

PHP-CLI promise implementation

1.2.5(5y ago)152.0k3BSD-3-ClausePHPPHP ^7.3CI failing

Since Mar 4Pushed 5y ago1 watchersCompare

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

READMEChangelog (8)Dependencies (8)Versions (10)Used By (0)

PHP Promises/A+ implementation
==============================

[](#php-promisesa-implementation)

[![PHP >= 7.2 ](https://camo.githubusercontent.com/a4f22f6711703a9e9345fe97cbda880918118648ceee1d89d5991489b9de9f66/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230372e322d626c75652e737667)](http://php.net)[![Swoole >= 4.2](https://camo.githubusercontent.com/d2db189b558f3a355cd09e130a504eb159a8cea8788450c6e06cde4023bf4d61/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d253345253344253230342e322d626c75652e737667)](https://github.com/swoole/swoole-src)[![Latest Stable Version](https://camo.githubusercontent.com/db0b0279ecb969f457d4dbd106a0635e2163c907e03a6c7db4d4f6f35d97a319/68747470733a2f2f706f7365722e707567782e6f72672f73747265616d636f6d6d6f6e2f70726f6d6973652f762f737461626c65)](https://packagist.org/packages/streamcommon/promise)[![Total Downloads](https://camo.githubusercontent.com/135a0dd0e7f358db87e9e98a4c708fb2ce5d17409174e771623c33a7b6cd97da/68747470733a2f2f706f7365722e707567782e6f72672f73747265616d636f6d6d6f6e2f70726f6d6973652f646f776e6c6f616473)](https://packagist.org/packages/streamcommon/promise)[![License](https://camo.githubusercontent.com/c5d394adb545d9b1a8296285920e2cb223ba8c43dd4501783c87f54b822f7ac2/68747470733a2f2f706f7365722e707567782e6f72672f73747265616d636f6d6d6f6e2f70726f6d6973652f6c6963656e7365)](./LICENSE)

This package provides [Promise/A+](https://github.com/promises-aplus/promises-spec) PHP implementation.

Branches
========

[](#branches)

[![Master](https://camo.githubusercontent.com/3fa4dd6b22d6ba39b4a6124ce262c7035f01b66bbcdce677bdbdb53665592249/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6272616e63682d6d61737465722d626c75652e737667)](https://github.com/streamcommon/promise/tree/master) [![Build Status](https://camo.githubusercontent.com/b56ab20c16940238328a829d36f2e666e3c50ddd8b0c1d881e7b88d6fc4d4528/68747470733a2f2f7472617669732d63692e6f72672f73747265616d636f6d6d6f6e2f70726f6d6973652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/streamcommon/promise) [![Coverage Status](https://camo.githubusercontent.com/37b1461770eed2b6be73f8a539194e552ed130c34b524081a7c44efe6c70ba1c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73747265616d636f6d6d6f6e2f70726f6d6973652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/streamcommon/promise?branch=master)

[![Develop](https://camo.githubusercontent.com/f9833e2ebd3338a783a834689428f7716e095680ae37b48ae7a950e73deef3f2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6272616e63682d646576656c6f702d626c75652e737667)](https://github.com/streamcommon/promise/tree/develop) [![Build Status](https://camo.githubusercontent.com/96a0649f94013d25b89f81963d3b78d460006c6b80e0756a394c51a064825355/68747470733a2f2f7472617669732d63692e6f72672f73747265616d636f6d6d6f6e2f70726f6d6973652e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/streamcommon/promise) [![Coverage Status](https://camo.githubusercontent.com/e8e20f4c8e69cc6aaf1783a491f30b908242ebe5999ed68744f0b30e697172c0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73747265616d636f6d6d6f6e2f70726f6d6973652f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/streamcommon/promise?branch=develop)

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

[](#installation)

Console run:

```
    composer require streamcommon/promise
```

Or add into your `composer.json`:

```
    "require": {
        "streamcommon/promise": "*"
    }
```

> If you want see TRUE promise then install [Swoole](http://php.net/manual/en/swoole.installation.php) extension. For more info visit the [Swoole repo](https://github.com/swoole/swoole-src)
>
> > NOTE: TRUE promise work only in CLI mode

Promise
-------

[](#promise)

Promise is a library which provides [Promise/A+](https://github.com/promises-aplus/promises-spec) PHP implementation.

All Promise it a special PHP classes that contains its state:

- `pending` - PromiseInterface::STATE\_PENDING
- `fulfilled` - PromiseInterface::STATE\_FULFILLED
- `rejected` - PromiseInterface::STATE\_REJECTED

To initiate a new promise, you can use static method `PromiseInterface::create` or create with new. All resulting `Promise` has `PromiseInterface::STATE_PENDING` state.

```
    $promise = new Promise(function(callable $resolve, callable $reject));
    // OR
    $promise = Promise::create(function(callable $resolve, callable $reject))
```

When `function($resolve, $reject)` executor finishes the job, it should call one of the functions:

- `$resolve` to indicate that the job finished successfully and set `Promise` state to `PromiseInterface::STATE_FULFILLED`

```
    $resolve = function ($value) {
        $this->setState(PromiseInterface::STATE_FULFILLED);
        $this->setResult($value);
    };
```

- `$reject` to indicate that an error occurred and set `Promise` state to `PromiseInterface::STATE_REJECTED`

```
    $reject = function ($value) {
        $this->setState(PromiseInterface::STATE_REJECTED);
        $this->setResult($value);
    };
```

Method `PromiseInterface::then()` it be called after promise change stage. In terms of our analogy: this is the “subscription".

```
    public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;
```

- `$onFulfilled` run when the `Promise` is resolved and it has `PromiseInterface::STATE_FULFILLED` state.
- `$onFulfilled` run when the `Promise` is rejected and it has `PromiseInterface::STATE_REJECTED` state.

> NOTE: If `$onFulfilled` or `$onFulfilled` is not a callable function it was ignore

Calling `PromiseInterface::resolve()` creates a successfully executed promise with the result value.

```
    public static function resolve($value): PromiseInterface;
```

It is similar to:

```
    $promise = new Promise(function(callable $resolve) {
        $resolve($value)
    });
```

Similarly `PromiseInterface::reject()` creates an already executed promise with an error value.

```
    public static function reject($value): PromiseInterface;
```

It is similar to:

```
    $promise = new Promise(function(callable $resolve, callable $reject) {
        $reject($value)
    });
```

Sub promise
-----------

[](#sub-promise)

When `function($resolve, $reject)` executor finishes the job, it can return `PromiseInterface`.

```
    $promise = Promise::create(function (callable $resolve) {
        $resolve(Promise::create(function (callable $subResolve) {
            $subResolve(42);
        }));
    });
```

In this case, it will wait for the execution of sub promise.

Method `PromiseInterface::then()` can return `PromiseInterface` to.

```
    $promise->then(function ($value) {
        return Promise::create(function (callable $resolve) use ($value) {
            $resolve($value + 1);
        });
    });
```

For more info check [example](/example) scripts.

Example
-------

[](#example)

> Standard Promise

```
    use Streamcommon\Promise\Promise;

    $promise = Promise::create(function (callable $resolve) {
        $resolve(41);
    });
    $newPromise = $promise->then(function ($value) {
        return $value + 1;
    });
    $promise->then(function ($value) {
        echo $value . ' === 41' . PHP_EOL;
    });
    $newPromise->then(function ($value) {
        echo $value . ' === 42' . PHP_EOL;
    });
    $promise->wait(); // promise execution
```

> If you want see TRUE promise then install [Swoole](http://php.net/manual/en/swoole.installation.php) extension. For more info visit the [Swoole repo](https://github.com/swoole/swoole-src)
>
> > NOTE: TRUE promise work only in CLI mode

```
    use Streamcommon\Promise\ExtSwoolePromise;

    // be careful with this
    \Swoole\Runtime::enableCoroutine(); // IF YOU WANT REALY ASYNC

    $promise = ExtSwoolePromise::create(function (callable $resolve) {
        // the function is executed automatically when the promise is constructed
        $resolve(41);
    });
    $promise->then(function ($value) {
        // the function is executed automatically after __constructor job
        return $value + 1;
    })->then(function ($value) {
        // the function is executed automatically after ::then()
        echo $value . PHP_EOL;
    });
```

> Sub promise

```
    use Streamcommon\Promise\Promise;

    $promise = Promise::create(function (callable $resolve) {
        $resolve(Promise::create(function (callable $resolve) {
            $resolve(42);
        }));
    });
    $newPromise = $promise->then(function ($value) {
        return $value + 1;
    });
    $superNewPromise = $promise->then(function ($value) {
        return Promise::create(function (callable $resolve) use ($value) {
            $resolve($value + 2);
        });
    });
    $promise->then(function ($value) {
        echo $value . ' === 42' . PHP_EOL;
    });
    $newPromise->then(function ($value) {
        echo $value . ' === 43' . PHP_EOL;
    });
    $superNewPromise->then(function ($value) {
        echo $value . ' === 44' . PHP_EOL;
    });
    $promise->wait();
```

> Sub async promise

```
    use Streamcommon\Promise\ExtSwoolePromise;

    // be careful with this
    \Swoole\Runtime::enableCoroutine(); // IF YOU WANT REALY ASYNC

    $promise = ExtSwoolePromise::create(function (callable $resolve) {
        $promise = ExtSwoolePromise::create(function (callable $resolve) {
            $resolve(41);
        });
        $promise->then(function ($value) use ($resolve) {
            $resolve($value);
        });
    });
    $promise->then(function ($value) {
        return $value + 1;
    })->then(function ($value) {
        echo $value . PHP_EOL;
    });
```

> If use `ExtSwoolePromise` with `daemon|cycle|loop` you must use `Swoole\Runtime::wait()`

```
    \Swoole\Runtime::enableCoroutine();
    while (true) {
        ///
        Some code with ExtSwoolePromise
        ///
        \Swoole\Runtime::wait();
    }
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 93.7% 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 ~68 days

Recently: every ~118 days

Total

8

Last Release

2147d ago

Major Versions

0.8.0 → 1.0.02019-03-06

PHP version history (2 changes)0.8.0PHP ^7.2

1.2.4PHP ^7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/3a435df1458c75a1a9760f611d3ba8058b3b6781ce55d3893249cb3fefd644ea?d=identicon)[streamcommon](/maintainers/streamcommon)

---

Top Contributors

[![aleksandr-yulin](https://avatars.githubusercontent.com/u/5064134?v=4)](https://github.com/aleksandr-yulin "aleksandr-yulin (74 commits)")[![TheIrritainer](https://avatars.githubusercontent.com/u/5379868?v=4)](https://github.com/TheIrritainer "TheIrritainer (5 commits)")

---

Tags

php-asyncphp-extensionphp7promisepromises-apluspromisesaplusswoolestreamcommonphp promise

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[simshaun/recurr

PHP library for working with recurrence rules

1.6k15.7M40](/packages/simshaun-recurr)[alchemy/zippy

Zippy, the archive manager companion

47522.6M51](/packages/alchemy-zippy)[commerceguys/tax

Tax library with a flexible data model, predefined tax rates, powerful resolving logic.

286763.3k](/packages/commerceguys-tax)[cron/cron-bundle

Symfony cron

1901.5M2](/packages/cron-cron-bundle)[sonata-project/classification-bundle

Symfony SonataClassificationBundle

913.2M20](/packages/sonata-project-classification-bundle)[sylius/promotion

Flexible promotion management for PHP applications.

28477.8k9](/packages/sylius-promotion)

PHPackages © 2026

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