PHPackages                             cubiche/async - 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. cubiche/async

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

cubiche/async
=============

Async library

126PHP

Since Apr 17Pushed 9y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (19)Used By (0)

Cubiche/Async
=============

[](#cubicheasync)

[![Build Status](https://camo.githubusercontent.com/4818264884d14b512dd1d3132d747538f9b0f264060cac128dc9235042eb86c2/68747470733a2f2f7472617669732d63692e6f72672f637562696368652f6173796e632e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/cubiche/async) [![Coverage Status](https://camo.githubusercontent.com/f6b6de495a27c25ce9ac0c1531434bf68169ee123538d0f3ae8fcbe2def7082d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f637562696368652f6173796e632f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/cubiche/async?branch=master) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/371be3454d0bd4378b5468552480d1e9676efeb707e72914cd009f42e55c3fcc/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f637562696368652f6173796e632f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/cubiche/async/?branch=master)

An Event loop abstraction layer, built on top of [React PHP/Event Loop](https://github.com/reactphp/event-loop), and a lightweight implementation of [Promises/A+](https://promisesaplus.com/) for PHP, inspired in [React PHP/Promise](https://github.com/reactphp/promise).

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

[](#installation)

Via [Composer](http://getcomposer.org/)

```
$ composer require cubiche/async:dev-master
```

The Promise API
---------------

[](#the-promise-api)

The [Promises/A+](https://promisesaplus.com/) proposal describes a promise as an interface for interacting with an object that represents the result of an action that is performed asynchronously, and may or may not be finished at any given point in time. The purpose of the promise object is to allow for interested parties to get access to the result of the deferred task when it completes.

Methods:

- `PromiseInterface::then(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null)` – regardless of when the promise was or will be resolved or rejected, then calls one of the `$onFulfilled` or `$onRejected` callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result value or rejection reason. Additionally, the `$onNotify` callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.

This method returns a new promise which is resolved or rejected via the return value of the `$onFulfilled` or `$onRejected` callbacks (unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining). It also notifies via the return value of the `$onNotify` callback.

- `PromiseInterface::done(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null)` – this method is similar to `PromiseInterface::then` but not returns a new promise. It will cause a fatal error if either `$onFulfilled`, `$onRejected` or `$onNotify` callbacks throw an exception.
- `PromiseInterface::otherwise(callable $onRejected)` – shorthand for `PromiseInterface::then(null, $onRejected)`.
- `PromiseInterface::always(callable $onFulfilledOrRejected, callable $onNotify = null)` – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved.
- `PromiseInterface::state()` – returns the promise state (`State::PENDING()`, `State::FULFILLED()`, or `State::REJECTED()`).

The Deferred API
----------------

[](#the-deferred-api)

The purpose of the deferred object is to expose the associated Promise instance as well as APIs that can be used for signaling the successful or unsuccessful completion, as well as the status of the task.

Methods:

- `DeferredInterface::resolve($value = null)` – resolves the derived promise with the `$value`.
- `DeferredInterface::reject($reason = null)` – rejects the derived promise with the `$reason`.
- `DeferredInterface::notify($state = null)` – provides updates on the status of the promise's execution. This may be called multiple times before the promise is either resolved or rejected.
- `DeferredInterface::cancel()` – rejects the derived promise, if it is posible, with `CancellationException` reason.
- `DeferredInterface::promise()` – returns the promise object associated with this deferred.

Basic usage
-----------

[](#basic-usage)

Usage example with [React PHP/Event Loop](https://github.com/reactphp/event-loop)

```
