Download the PHP package react/promise without Composer

On this page you can find all versions of the php package react/promise. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

All versions of promise with dependencies

Informations about the package promise

Promise

A lightweight implementation of CommonJS Promises/A for PHP.

CI status installs on Packagist

Table of Contents

  1. Introduction
  2. Concepts
    • Deferred
    • Promise
  3. API
    • Deferred
      • Deferred::promise()
      • Deferred::resolve()
      • Deferred::reject()
      • Deferred::notify()
    • PromiseInterface
      • PromiseInterface::then()
    • ExtendedPromiseInterface
      • ExtendedPromiseInterface::done()
      • ExtendedPromiseInterface::otherwise()
      • ExtendedPromiseInterface::always()
      • ExtendedPromiseInterface::progress()
    • CancellablePromiseInterface
      • CancellablePromiseInterface::cancel()
    • Promise
    • FulfilledPromise
    • RejectedPromise
    • LazyPromise
    • Functions
      • resolve()
      • reject()
      • all()
      • race()
      • any()
      • some()
      • map()
      • reduce()
    • PromisorInterface
  4. Examples
    • How to use Deferred
    • How promise forwarding works
      • Resolution forwarding
      • Rejection forwarding
      • Mixed resolution and rejection forwarding
      • Progress event forwarding
    • done() vs. then()
  5. Install
  6. Credits
  7. License

Introduction

Promise is a library implementing CommonJS Promises/A for PHP.

It also provides several other useful promise-related concepts, such as joining multiple promises and mapping and reducing collections of promises.

If you've never heard about promises before, read this first.

Concepts

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

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

Deferred

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

The promise method returns the promise of the deferred.

The resolve and reject methods control the state of the deferred.

The deprecated notify method is for progress notification.

The constructor of the Deferred accepts an optional $canceller argument. See Promise for more information.

Deferred::promise()

Returns the promise of the deferred, which you can hand out to others while keeping the authority to modify its state to yourself.

Deferred::resolve()

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

If $value itself is a promise, the promise will transition to the state of this promise once it is resolved.

Deferred::reject()

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.

If $reason itself is a promise, the promise will be rejected with the outcome of this promise regardless whether it fulfills or rejects.

Deferred::notify()

Deprecated in v2.6.0: Progress support is deprecated and should not be used anymore.

Triggers progress notifications, to indicate to consumers that the computation is making progress toward its result.

All consumers are notified by having $onProgress (which they registered via $promise->then()) called with $update.

PromiseInterface

The promise interface provides the common interface for all promise implementations.

A promise represents an eventual outcome, which is either fulfillment (success) and an associated value, or rejection (failure) and an associated reason.

Once in the fulfilled or rejected state, a promise becomes immutable. Neither its state nor its result (or error) can be modified.

Implementations

PromiseInterface::then()

Transforms a promise's value by applying a function to the promise's fulfillment or rejection value. Returns a new promise for the transformed result.

The then() method registers new fulfilled, rejection and progress handlers with a promise (all parameters are optional):

It returns a new promise that will fulfill with the return value of either $onFulfilled or $onRejected, whichever is called, or will reject with the thrown exception if either throws.

A promise makes the following guarantees about handlers registered in the same call to then():

  1. Only one of $onFulfilled or $onRejected will be called, never both.
  2. $onFulfilled and $onRejected will never be called more than once.
  3. $onProgress (deprecated) may be called multiple times.

See also

ExtendedPromiseInterface

The ExtendedPromiseInterface extends the PromiseInterface with useful shortcut and utility methods which are not part of the Promises/A specification.

Implementations

ExtendedPromiseInterface::done()

Consumes the promise's ultimate value if the promise fulfills, or handles the ultimate error.

It will cause a fatal error if either $onFulfilled or $onRejected throw or return a rejected promise.

Since the purpose of done() is consumption rather than transformation, done() always returns null.

See also

ExtendedPromiseInterface::otherwise()

Registers a rejection handler for promise. It is a shortcut for:

Additionally, you can type hint the $reason argument of $onRejected to catch only specific errors.

ExtendedPromiseInterface::always()

Allows you to execute "cleanup" type tasks in a promise chain.

It arranges for $onFulfilledOrRejected to be called, with no arguments, when the promise is either fulfilled or rejected.

always() behaves similarly to the synchronous finally statement. When combined with otherwise(), always() allows you to write code that is similar to the familiar synchronous catch/finally pair.

Consider the following synchronous code:

Similar asynchronous code (with doSomething() that returns a promise) can be written:

ExtendedPromiseInterface::progress()

Deprecated in v2.6.0: Progress support is deprecated and should not be used anymore.

Registers a handler for progress updates from promise. It is a shortcut for:

CancellablePromiseInterface

A cancellable promise provides a mechanism for consumers to notify the creator of the promise that they are not longer interested in the result of an operation.

CancellablePromiseInterface::cancel()

The cancel() method notifies the creator of the promise that there is no further interest in the results of the operation.

Once a promise is settled (either fulfilled or rejected), calling cancel() on a promise has no effect.

Implementations

Promise

Creates a promise whose state is controlled by the functions passed to $resolver.

The promise constructor receives a resolver function and an optional canceller function which both will be called with 3 arguments:

If the resolver or canceller throw an exception, the promise will be rejected with that thrown exception as the rejection reason.

The resolver function will be called immediately, the canceller function only once all consumers called the cancel() method of the promise.

FulfilledPromise

Deprecated in v2.8.0: External usage of FulfilledPromise is deprecated, use resolve() instead.

Creates a already fulfilled promise.

Note, that $value cannot be a promise. It's recommended to use resolve() for creating resolved promises.

RejectedPromise

Deprecated in v2.8.0: External usage of RejectedPromise is deprecated, use reject() instead.

Creates a already rejected promise.

Note, that $reason cannot be a promise. It's recommended to use reject() for creating rejected promises.

LazyPromise

Deprecated in v2.8.0: LazyPromise is deprecated and should not be used anymore.

Creates a promise which will be lazily initialized by $factory once a consumer calls the then() method.

Functions

Useful functions for creating, joining, mapping and reducing collections of promises.

All functions working on promise collections (like all(), race(), some() etc.) support cancellation. This means, if you call cancel() on the returned promise, all promises in the collection are cancelled. If the collection itself is a promise which resolves to an array, this promise is also cancelled.

resolve()

Creates a promise for the supplied $promiseOrValue.

If $promiseOrValue is a value, it will be the resolution value of the returned promise.

If $promiseOrValue is a thenable (any object that provides a then() method), a trusted promise that follows the state of the thenable is returned.

If $promiseOrValue is a promise, it will be returned as is.

Note: The promise returned is always a promise implementing ExtendedPromiseInterface. If you pass in a custom promise which only implements PromiseInterface, this promise will be assimilated to a extended promise following $promiseOrValue.

reject()

Creates a rejected promise for the supplied $promiseOrValue.

If $promiseOrValue is a value, it will be the rejection value of the returned promise.

If $promiseOrValue is a promise, its completion value will be the rejected value of the returned promise.

This can be useful in situations where you need to reject a promise without throwing an exception. For example, it allows you to propagate a rejection with the value of another promise.

all()

Returns a promise that will resolve only once all the items in $promisesOrValues have resolved. The resolution value of the returned promise will be an array containing the resolution values of each of the items in $promisesOrValues.

race()

Initiates a competitive race that allows one winner. Returns a promise which is resolved in the same way the first settled promise resolves.

any()

Returns a promise that will resolve when any one of the items in $promisesOrValues resolves. The resolution value of the returned promise will be the resolution value of the triggering item.

The returned promise will only reject if all items in $promisesOrValues are rejected. The rejection value will be an array of all rejection reasons.

The returned promise will also reject with a React\Promise\Exception\LengthException if $promisesOrValues contains 0 items.

some()

Returns a promise that will resolve when $howMany of the supplied items in $promisesOrValues resolve. The resolution value of the returned promise will be an array of length $howMany containing the resolution values of the triggering items.

The returned promise will reject if it becomes impossible for $howMany items to resolve (that is, when (count($promisesOrValues) - $howMany) + 1 items reject). The rejection value will be an array of (count($promisesOrValues) - $howMany) + 1 rejection reasons.

The returned promise will also reject with a React\Promise\Exception\LengthException if $promisesOrValues contains less items than $howMany.

map()

Traditional map function, similar to array_map(), but allows input to contain promises and/or values, and $mapFunc may return either a value or a promise.

The map function receives each item as argument, where item is a fully resolved value of a promise or value in $promisesOrValues.

reduce()

Traditional reduce function, similar to array_reduce(), but input may contain promises and/or values, and $reduceFunc may return either a value or a promise, and $initialValue may be a promise or a value for the starting value.

PromisorInterface

The React\Promise\PromisorInterface provides a common interface for objects that provide a promise. React\Promise\Deferred implements it, but since it is part of the public API anyone can implement it.

Examples

How to use Deferred

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

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".

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)

Mixed resolution and rejection forwarding

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

Progress event forwarding

Deprecated in v2.6.0: Progress support is deprecated and should not be used anymore.

In the same way as resolution and rejection handlers, your progress handler MUST return a progress event to be propagated to the next link in the chain. If you return nothing, null will be propagated.

Also in the same way as resolutions and rejections, if you don't register a progress handler, the update will be propagated through.

If your progress handler throws an exception, the exception will be propagated to the next link in the chain. The best thing to do is to ensure your progress handlers do not throw exceptions.

This gives you the opportunity to transform progress events at each step in the chain so that they are meaningful to the next step. It also allows you to choose not to transform them, and simply let them propagate untransformed, by not registering a progress handler.

done() vs. then()

The golden rule is:

Either return your promise, or call done() on it.

At a first glance, then() and done() seem very similar. However, there are important distinctions.

The intent of then() is to transform a promise's value and to pass or return a new promise for the transformed value along to other parts of your code.

The intent of done() is to consume a promise's value, transferring responsibility for the value to your code.

In addition to transforming a value, then() allows you to recover from, or propagate intermediate errors. Any errors that are not handled will be caught by the promise machinery and used to reject the promise returned by then().

Calling done() transfers all responsibility for errors to your code. If an error (either a thrown exception or returned rejection) escapes the $onFulfilled or $onRejected callbacks you provide to done, it will be rethrown in an uncatchable way causing a fatal error.

Note that if a rejection value is not an instance of \Exception, it will be wrapped in an exception of the type React\Promise\UnhandledRejectionException.

You can get the original rejection reason by calling $exception->getReason().

Install

The recommended way to install this library is through Composer. New to Composer?

This project follows SemVer. This will install the latest supported version:

See also the CHANGELOG for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.4 through current PHP 8+ and HHVM. It's highly recommended to use the latest supported PHP version for this project.

Credits

Promise is a port of when.js by Brian Cavalier.

Also, large parts of the documentation have been ported from the when.js Wiki and the API docs.

License

Released under the MIT license.


Version
Requires php Version >=5.4.0

The package react/promise contains the following files

Loading the files please wait ....