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

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

gandung/promise
===============

Promise A+ Implementation In PHP

v1.0.2(8y ago)1017[1 issues](https://github.com/plvhx/promise/issues)BSD-3-ClausePHPPHP &gt;=5.6.0 || &gt;=7.1.0CI failing

Since Aug 16Pushed 5y ago4 watchersCompare

[ Source](https://github.com/plvhx/promise)[ Packagist](https://packagist.org/packages/gandung/promise)[ RSS](/packages/gandung-promise/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)Dependencies (2)Versions (5)Used By (0)

Promises/A+ Implementation In PHP
=================================

[](#promisesa-implementation-in-php)

[![Minimum PHP Version](https://camo.githubusercontent.com/86e7d829a466cacd5658a22073e27d49d39dac72cc18216ac4963ed5463c5bbc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e362d3838393242462e7376673f7374796c653d666c61742d737175617265)](https://php.net/)[![Build status](https://camo.githubusercontent.com/2aeab716c0a00f0ad73d3f6115187952cafa40e5eec65a50c96d46d4789cc223/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f386d78356a3832306c326673717034313f7376673d74727565)](https://ci.appveyor.com/project/plvhx/promise)[![SensioLabsInsight](https://camo.githubusercontent.com/0a4ca1e4bca307e05955748d3eb779e09032e1a6fba5d1186c3334536fc65eca/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f64653239333965632d366637632d346639372d383963612d3437396164633933346330342f6d696e692e706e67)](https://insight.sensiolabs.com/projects/de2939ec-6f7c-4f97-89ca-479adc934c04)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/d4a34bbe85e63d25ccfe51631b52878d29825d822196b6085b322898426ae1b7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f706c7668782f70726f6d6973652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/plvhx/promise/?branch=master)

[ ![Promises/A+ logo](https://camo.githubusercontent.com/7e823e22ecb853c4970d0a22f883de1b2f2607233f9731376ef859ff6c9364b2/68747470733a2f2f70726f6d6973657361706c75732e636f6d2f6173736574732f6c6f676f2d736d616c6c2e706e67 "Promises/A+ 1.0 compliant")](https://promisesaplus.com/)This is a [Promises/A+](https://promisesaplus.com) implementation in PHP. This handles promise chaining immutably until all context handler is all gone from execution context.

Features
--------

[](#features)

- Promises/A+ implementation.
- Promise resolution and chaining is handled iteratively.

API
===

[](#api)

Deferred
========

[](#deferred)

Quick Start
-----------

[](#quick-start)

```
use Gandung\Promise\Deferred;

$deferred = new Deferred();

$deferred->resolve('success.');

$deferred->promise()->then(
	function($d) {
		echo sprintf("%s" . PHP_EOL, $d);
	},
	function($e) {
		echo sprintf("%s" . PHP_EOL, $e);
	}
);
```

It will print "success." because the promise state is fulfilled.

```
use Gandung\Promise\Deferred;

$deferred = new Deferred();

$deferred->reject('fail.');

$deferred->promise()->then(
	function($d) {
		echo sprintf("%s" . PHP_EOL, $d);
	},
	function($e) {
		echo sprintf("%s" . PHP_EOL, $e);
	}
);
```

It will print "fail." because the promise state is rejected.

Deferred Method
===============

[](#deferred-method)

- `promise()`

Description
-----------

[](#description)

Ability to return immutable promise object when current is null.

Return Value
------------

[](#return-value)

Returns the promise of the deferred.

- `resolve($value)`

Description
-----------

[](#description-1)

Resolves the promise return by method `promise()`. All consumers are notified by having `$onFulfilled` (which they registered via `$promise->then()`) called with `$value`.

Return Value
------------

[](#return-value-1)

None.

- `reject($reason)`

Description
-----------

[](#description-2)

Rejects the promise returned by `promise()`, signalling that the deferred's computation failed. All consumers are notified by having `$onRejected` (which they registered via `$promise->then()`) called with `$reason`.

Return Value
------------

[](#return-value-2)

None.

Promise
=======

[](#promise)

Quick Start
-----------

[](#quick-start-1)

```
use Gandung\Promise\Promise;

$promise = new Promise();

$promise->then(
	function($d) {
		echo sprintf("%s" . PHP_EOL, $d);
	},
	function($e) {
		throw new \Exception($e);
	}
);
```

Promise Method
==============

[](#promise-method)

- `then(callable $onFulfilled, callable $onRejected)`

Description
-----------

[](#description-3)

Appends fulfillment and rejection handlers to the promise, and returns an immutable new promise resolving to the return value of the called handler.

Return Value
------------

[](#return-value-3)

An immutable new promise.

- `resolve($value)`

Description
-----------

[](#description-4)

Fulfills the promise with the given '$value'

Return Value
------------

[](#return-value-4)

None.

- `reject($reason)`

Description
-----------

[](#description-5)

Rejects the promise with the given '$reason'

Return Value
------------

[](#return-value-5)

None.

- `currentState()`

Description
-----------

[](#description-6)

Returns the state of the promise. List of promise state can be found in 'PromiseInterface' class.

Return Value
------------

[](#return-value-6)

Either STATE\_PENDING, STATE\_FULFILLED, OR STATE\_REJECTED

- `wait()`

Description
-----------

[](#description-7)

Synchronously wait on the current promise to complete it's unfulfilled tasks.

Return Value
------------

[](#return-value-7)

Returning the most current resolved promise value.

- `cancel()`

Description
-----------

[](#description-8)

Possible to cancel the most current active promise and it's ancestors which not yet been resolved and leave the current active promise state to rejected. It means the most current active promise is unable to be used.

Return Value
------------

[](#return-value-8)

Return the most current cancelled promise value. It can be throwing an exception.

- `setWaitCallback(\Closure $callback = null)`

Description
-----------

[](#description-9)

Set the wait callback handler.

Return Value
------------

[](#return-value-9)

None.

- `setCancelCallback(\Closure $callback = null)`

Description
-----------

[](#description-10)

Set the cancellation callback handler.

Return Value
------------

[](#return-value-10)

None.

FulfilledPromise same as Promise
================================

[](#fulfilledpromise-same-as-promise)

Quick Start
-----------

[](#quick-start-2)

```
use Gandung\Promise\FulfilledPromise;

$promise = new FulfilledPromise('the quick dirty brown fox.');

$promise->then(function($d) {
	echo sprintf("%s" . PHP_EOL, $d);
});
```

RejectedPromise same as Promise
===============================

[](#rejectedpromise-same-as-promise)

Quick Start
-----------

[](#quick-start-3)

```
use Gandung\Promise\RejectedPromise;

$promise = new RejectedPromise('the quick dirty brown fox.');

$promise->then(null, function($e) {
	throw new \Exception($e);
});
```

Contributors
============

[](#contributors)

- [Paulus Gandung Prakosa](https://github.com/plvhx)

License
=======

[](#license)

BSD 3-Clause License

Copyright (c) 2017, Paulus Gandung Prakosa All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity61

Established project with proven stability

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

Total

3

Last Release

3148d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e1b54fcf07f817bc79b2aa1f4416a29d74e1bf461f0b7fe2804762402b83b935?d=identicon)[gandung](/maintainers/gandung)

---

Top Contributors

[![plvhx](https://avatars.githubusercontent.com/u/12740518?v=4)](https://github.com/plvhx "plvhx (41 commits)")

---

Tags

hacktoberfest

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[paulredmond/html-to-amp

A PHP Library to Convert HTML to AMP HTML

6826.0k](/packages/paulredmond-html-to-amp)[mageprince/module-buynow

Magento2 Buy Now Module

829.7k](/packages/mageprince-module-buynow)[netzmacht/php-leaflet

PHP leaflet definition and javascript generator

1927.2k2](/packages/netzmacht-php-leaflet)[edofre/laravel-fullcalendar-scheduler

Laravel component for fullcalendar scheduler module

251.5k](/packages/edofre-laravel-fullcalendar-scheduler)

PHPackages © 2026

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