Download the PHP package react/async without Composer

On this page you can find all versions of the php package react/async. 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?

Informations about the package async

Async Utilities

CI status installs on Packagist

Async utilities and fibers for ReactPHP.

This library allows you to manage async control flow. It provides a number of combinators for Promise-based APIs. Instead of nesting or chaining promise callbacks, you can declare them as a list, which is resolved sequentially in an async manner. React/Async will not automagically change blocking code to be async. You need to have an actual event loop and non-blocking libraries interacting with that event loop for it to work. As long as you have a Promise-based API that runs in an event loop, it can be used with this library.

Table of Contents

Usage

This lightweight library consists only of a few simple functions. All functions reside under the React\Async namespace.

The below examples refer to all functions with their fully-qualified names like this:

As of PHP 5.6+ you can also import each required function into your code like this:

Alternatively, you can also use an import statement similar to this:

async()

The async(callable():(PromiseInterface<T>|T) $function): (callable():PromiseInterface<T>) function can be used to return an async function for a function that uses await() internally.

This function is specifically designed to complement the await() function. The await() function can be considered blocking from the perspective of the calling code. You can avoid this blocking behavior by wrapping it in an async() function call. Everything inside this function will still be blocked, but everything outside this function can be executed asynchronously without blocking:

See also the await() function for more details.

Note that this function only works in tandem with the await() function. In particular, this function does not "magically" make any blocking function non-blocking:

As an alternative, you should always make sure to use this function in tandem with the await() function and an async API returning a promise as shown in the previous example.

The async() function is specifically designed for cases where it is used as a callback (such as an event loop timer, event listener, or promise callback). For this reason, it returns a new function wrapping the given $function instead of directly invoking it and returning its value.

You can invoke this wrapping function to invoke the given $function with any arguments given as-is. The function will always return a Promise which will be fulfilled with whatever your $function returns. Likewise, it will return a promise that will be rejected if you throw an Exception or Throwable from your $function. This allows you to easily create Promise-based functions:

The previous example uses await() inside a loop to highlight how this vastly simplifies consuming asynchronous operations. At the same time, this naive example does not leverage concurrent execution, as it will essentially "await" between each operation. In order to take advantage of concurrent execution within the given $function, you can "await" multiple promises by using a single await() together with Promise-based primitives like this:

The returned promise is implemented in such a way that it can be cancelled when it is still pending. Cancelling a pending promise will cancel any awaited promises inside that fiber or any nested fibers. As such, the following example will only output ab and cancel the pending delay(). The await() calls in this example would throw a RuntimeException from the cancelled delay() call that bubbles up through the fibers.

await()

The await(PromiseInterface<T> $promise): T function can be used to block waiting for the given $promise to be fulfilled.

This function will only return after the given $promise has settled, i.e. either fulfilled or rejected. While the promise is pending, this function can be considered blocking from the perspective of the calling code. You can avoid this blocking behavior by wrapping it in an async() function call. Everything inside this function will still be blocked, but everything outside this function can be executed asynchronously without blocking:

See also the async() function for more details.

Once the promise is fulfilled, this function will return whatever the promise resolved to.

Once the promise is rejected, this will throw whatever the promise rejected with. If the promise did not reject with an Exception or Throwable, then this function will throw an UnexpectedValueException instead.

coroutine()

The coroutine(callable(mixed ...$args):(\Generator|PromiseInterface<T>|T) $function, mixed ...$args): PromiseInterface<T> function can be used to execute a Generator-based coroutine to "await" promises.

Using Generator-based coroutines is an alternative to directly using the underlying promise APIs. For many use cases, this makes using promise-based APIs much simpler, as it resembles a synchronous code flow more closely. The above example performs the equivalent of directly using the promise APIs:

The yield keyword can be used to "await" a promise resolution. Internally, it will turn the entire given $function into a Generator. This allows the execution to be interrupted and resumed at the same place when the promise is fulfilled. The yield statement returns whatever the promise is fulfilled with. If the promise is rejected, it will throw an Exception or Throwable.

The coroutine() function will always return a Promise which will be fulfilled with whatever your $function returns. Likewise, it will return a promise that will be rejected if you throw an Exception or Throwable from your $function. This allows you to easily create Promise-based functions:

The previous example uses a yield statement inside a loop to highlight how this vastly simplifies consuming asynchronous operations. At the same time, this naive example does not leverage concurrent execution, as it will essentially "await" between each operation. In order to take advantage of concurrent execution within the given $function, you can "await" multiple promises by using a single yield together with Promise-based primitives like this:

delay()

The delay(float $seconds): void function can be used to delay program execution for duration given in $seconds.

This function will only return after the given number of $seconds have elapsed. If there are no other events attached to this loop, it will behave similar to PHP's sleep() function.

Unlike PHP's sleep() function, this function may not necessarily halt execution of the entire process thread. Instead, it allows the event loop to run any other events attached to the same loop until the delay returns:

This behavior is especially useful if you want to delay the program execution of a particular routine, such as when building a simple polling or retry mechanism:

Because this function only returns after some time has passed, it can be considered blocking from the perspective of the calling code. You can avoid this blocking behavior by wrapping it in an async() function call. Everything inside this function will still be blocked, but everything outside this function can be executed asynchronously without blocking:

See also the async() function for more details.

Internally, the $seconds argument will be used as a timer for the loop so that it keeps running until this timer triggers. This implies that if you pass a really small (or negative) value, it will still start a timer and will thus trigger at the earliest possible time in the future.

The function is implemented in such a way that it can be cancelled when it is running inside an async() function. Cancelling the resulting promise will clean up any pending timers and throw a RuntimeException from the pending delay which in turn would reject the resulting promise.

parallel()

The parallel(iterable<callable():PromiseInterface<T>> $tasks): PromiseInterface<array<T>> function can be used like this:

series()

The series(iterable<callable():PromiseInterface<T>> $tasks): PromiseInterface<array<T>> function can be used like this:

waterfall()

The waterfall(iterable<callable(mixed=):PromiseInterface<T>> $tasks): PromiseInterface<T> function can be used like this:

Todo

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 from this branch:

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 PHP 8.1+. It's highly recommended to use the latest supported PHP version for this project.

We're committed to providing long-term support (LTS) options and to provide a smooth upgrade path. If you're using an older PHP version, you may use the 3.x branch (PHP 7.1+) or 2.x branch (PHP 5.3+) which both provide a compatible API but do not take advantage of newer language features. You may target multiple versions at the same time to support a wider range of PHP versions like this:

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

To run the test suite, go to the project root and run:

On top of this, we use PHPStan on max level to ensure type safety across the project:

License

MIT, see LICENSE file.

This project is heavily influenced by async.js.


All versions of async with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
react/event-loop Version ^1.2
react/promise Version ^3.0 || ^2.8 || ^1.2.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package react/async contains the following files

Loading the files please wait ....