PHPackages                             skelan/simple-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. [Queues &amp; Workers](/categories/queues)
4. /
5. skelan/simple-promise

ActiveLibrary[Queues &amp; Workers](/categories/queues)

skelan/simple-promise
=====================

A promise package that it is lightweight implementation

v0.2.4(3y ago)116MITPHPPHP ^7.0 || ^8.0

Since Jul 21Pushed 3y ago1 watchersCompare

[ Source](https://github.com/coding-lover/simple-promise)[ Packagist](https://packagist.org/packages/skelan/simple-promise)[ RSS](/packages/skelan-simple-promise/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (12)Used By (0)

Promise
=======

[](#promise)

A lightweight implementation of [CommonJS Promises/A](http://wiki.commonjs.org/wiki/Promises/A) for PHP.

This file mostly code come from [react/promise](https://github.com/reactphp/promise), thanks reactphp Team provide such a useful package.

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

[](#installation)

Make sure that you have composer installed [Composer](http://getcomposer.org/).

If you don't have Composer run the below command

```
curl -sS https://getcomposer.org/installer | php
```

Run the installation

```
composer require skelan/simple-promise
```

Concepts
--------

[](#concepts)

### Deferred

[](#deferred)

A **Deferred** represents a computation or unit of work that may not have completed yet. Typically (but not always), that computation will be something that executes asynchronously and completes at some point in the future.

### Promise

[](#promise-1)

While a deferred represents the computation itself, a **Promise** represents the result of that computation. Thus, each deferred has a promise that acts as a placeholder for its actual result.

API
---

[](#api)

### Deferred

[](#deferred-1)

A deferred represents an operation whose resolution is pending. It has separate promise and resolver parts.

```
$deferred = new Skelan\SimplePromise\Deferred();

$promise = $deferred->promise();

$deferred->resolve(mixed $value = null);
$deferred->reject(mixed $reason = null);
```

### How promise forwarding works

[](#how-promise-forwarding-works)

A few simple examples to show how the mechanics of Promises/A forwarding works. These examples are contrived, of course, and in real usage, promise chains will typically be spread across several function calls, or even several levels of your application architecture.

#### Resolution forwarding

[](#resolution-forwarding)

Resolved promises forward resolution values to the next promise. The first promise, `$deferred->promise()`, will resolve with the value passed to `$deferred->resolve()` below.

Each call to `then()` returns a new promise that will resolve with the return value of the previous handler. This creates a promise "pipeline".

```
$deferred = new Skelan\SimplePromise\Deferred();

$deferred->promise()
    ->then(function ($x) {
        // $x will be the value passed to $deferred->resolve() below
        // and returns a *new promise* for $x + 1
        return $x + 1;
    })
    ->then(function ($x) {
        // $x === 2
        // This handler receives the return value of the
        // previous handler.
        return $x + 1;
    })
    ->then(function ($x) {
        // $x === 3
        // This handler receives the return value of the
        // previous handler.
        return $x + 1;
    })
    ->then(function ($x) {
        // $x === 4
        // This handler receives the return value of the
        // previous handler.
        echo 'Resolve ' . $x;
    });

$deferred->resolve(1); // Prints "Resolve 4"
```

#### Rejection forwarding

[](#rejection-forwarding)

Rejected promises behave similarly, and also work similarly to try/catch: When you catch an exception, you must rethrow for it to propagate.

Similarly, when you handle a rejected promise, to propagate the rejection, "rethrow" it by either returning a rejected promise, or actually throwing (since promise translates thrown exceptions into rejections)

```
$deferred = new Skelan\SimplePromise\Deferred();

$deferred->promise()
    ->then(function ($x) {
        throw new \Exception($x + 1);
    })
    ->otherwise(function (\Exception $x) {
        // Propagate the rejection
        throw $x;
    })
    ->otherwise(function ($x) {
        echo 'Reject ' . $x->getMessage(); // 3
    });

$deferred->resolve(1);  // Prints "Reject 3"
```

### Best practices

[](#best-practices)

#### 1) Try/catch demo

[](#1-trycatch-demo)

Just like try/catch, you can choose to propagate or not. Mixing resolutions and rejections will still forward handler results in a predictable way.

```
try {
  return doSomething();
} catch(\Exception $e) {
    return handleError($e);
} finally {
    doFinally();
}
```

```
$deferred = new Skelan\SimplePromise\Deferred();

$deferred->promise()
    ->then(function ($x) {
        return $x + 1;
    })
    ->then(function ($x) {
        throw new \Exception($x + 1);
    })
    ->otherwise(function (\Exception $x) {
        //catch exception
        var_dump('otherwise: ' . ($x->getMessage() + 1)); //4
    })
    ->always(function () {
        //finally
        var_dump('finally ');
    });

$deferred->resolve(1);
```

#### 2) Asynchronous call

[](#2-asynchronous-call)

[swoole is awesome extension of php](https://github.com/swoole/swoole-src)

```
function remoteRequest() {
    $deferred = new \Skelan\SimplePromise\Deferred(function (\Skelan\SimplePromise\PromiseInterface $promise) {
        var_dump('call cancel.');
    });

    \Swoole\Timer::after(1000, function() use($deferred) {
        $deferred->resolve('finish: ' . time());
    });

    return $deferred->promise();
}

remoteRequest()->then(function ($value) {
    var_dump('resolve: ' . time());
    var_dump($value);
    throw new \Exception('xxx');
}, function($reason) {
    var_dump($reason);
})->otherwise(function(\Throwable $exception) {
    var_dump('exception: ' . $exception->getMessage());
})->always(function($value) {
    var_dump('otherwise: ' . $value);
});
```

License
-------

[](#license)

Released under the [MIT](LICENSE) license.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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 ~1 days

Total

9

Last Release

1383d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a4d6ef240faa372da241e7835b02d04a3ed1eccb177915427a924212cf085aa5?d=identicon)[coding-lover](/maintainers/coding-lover)

---

Top Contributors

[![coding-lover](https://avatars.githubusercontent.com/u/9207532?v=4)](https://github.com/coding-lover "coding-lover (1 commits)")

---

Tags

promiseresolveresolverdeferredcancelrejectcanceller

### Embed Badge

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

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

###  Alternatives

[amphp/amp

A non-blocking concurrency framework for PHP applications.

4.4k123.4M322](/packages/amphp-amp)[react/promise

A lightweight implementation of CommonJS Promises/A for PHP

2.5k361.9M563](/packages/react-promise)[php-http/promise

Promise used for asynchronous HTTP requests

1.8k308.8M32](/packages/php-http-promise)[react/promise-timer

A trivial implementation of timeouts for Promises, built on top of ReactPHP.

34141.9M96](/packages/react-promise-timer)[sabre/event

sabre/event is a library for lightweight event-based programming

35227.4M25](/packages/sabre-event)[amphp/dns

Async DNS resolution for Amp.

19239.2M41](/packages/amphp-dns)

PHPackages © 2026

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