PHPackages                             korot1245/block-react - 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. korot1245/block-react

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

korot1245/block-react
=====================

Lightweight library that eases integrating async components built for ReactPHP in a traditional, blocking environment.

v1.0.0(1y ago)0121MITPHPPHP &gt;=5.3

Since Feb 21Pushed 1y agoCompare

[ Source](https://github.com/korot1245/reactphp-block)[ Packagist](https://packagist.org/packages/korot1245/block-react)[ Docs](https://github.com/korot1245/reactphp-block)[ Fund](https://clue.engineering/support)[ GitHub Sponsors](https://github.com/clue)[ RSS](/packages/korot1245-block-react/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (1)

Deprecation notice
==================

[](#deprecation-notice)

This package has now been migrated over to [reactphp/async](https://github.com/reactphp/async)and only exists for BC reasons.

```
composer require "react/async:^4 || ^3 || ^2"
```

Only the `await()` function has been merged without its optional parameters `$loop` and `$timeout`, the rest of `await()` works as-is from the latest `v1.5.0` release with no other significant changes. Simply update your code to use the updated namespace like this:

```
// old
$result = Clue\React\Block\await($promise);

// new
$result = React\Async\await($promise);
```

See [reactphp/async](https://github.com/reactphp/async#usage) for more details.

The below documentation applies to the last release of this package. Further development will take place in the updated [reactphp/async](https://github.com/reactphp/async), so you're highly recommended to upgrade as soon as possible.

Legacy clue/reactphp-block
==========================

[](#legacy-cluereactphp-block)

[![CI status](https://github.com/clue/reactphp-block/workflows/CI/badge.svg)](https://github.com/clue/reactphp-block/actions)[![installs on Packagist](https://camo.githubusercontent.com/12fc8667a5b0614f88d66cf9a5cfd84cd1e9ed2a5001dae26bc15bc9a33e9487/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636c75652f626c6f636b2d72656163743f636f6c6f723d626c7565266c6162656c3d696e7374616c6c732532306f6e2532305061636b6167697374)](https://packagist.org/packages/clue/block-react)

Lightweight library that eases integrating async components built for [ReactPHP](https://reactphp.org/) in a traditional, blocking environment.

[ReactPHP](https://reactphp.org/) provides you a great set of base components and a huge ecosystem of third party libraries in order to perform async operations. The event-driven paradigm and asynchronous processing of any number of streams in real time enables you to build a whole new set of application on top of it. This is great for building modern, scalable applications from scratch and will likely result in you relying on a whole new software architecture.

But let's face it: Your day-to-day business is unlikely to allow you to build everything from scratch and ditch your existing production environment. This is where this library comes into play:

*Let's block ReactPHP*More specifically, this library eases the pain of integrating async components into your traditional, synchronous (blocking) application stack.

**Table of contents**

- [Support us](#support-us)
- [Quickstart example](#quickstart-example)
- [Usage](#usage)
    - [sleep()](#sleep)
    - [await()](#await)
    - [awaitAny()](#awaitany)
    - [awaitAll()](#awaitall)
- [Install](#install)
- [Tests](#tests)
- [License](#license)

Support us
----------

[](#support-us)

We invest a lot of time developing, maintaining and updating our awesome open-source projects. You can help us sustain this high-quality of our work by [becoming a sponsor on GitHub](https://github.com/sponsors/clue). Sponsors get numerous benefits in return, see our [sponsoring page](https://github.com/sponsors/clue)for details.

Let's take these projects to the next level together! 🚀

### Quickstart example

[](#quickstart-example)

The following example code demonstrates how this library can be used along with an [async HTTP client](https://github.com/reactphp/http#client-usage) to process two non-blocking HTTP requests and block until the first (faster) one resolves.

```
function blockingExample()
{
    // this example uses an HTTP client
    // this could be pretty much everything that binds to an event loop
    $browser = new React\Http\Browser();

    // set up two parallel requests
    $request1 = $browser->get('http://www.google.com/');
    $request2 = $browser->get('http://www.google.co.uk/');

    // keep the loop running (i.e. block) until the first response arrives
    $fasterResponse = Clue\React\Block\awaitAny(array($request1, $request2));

    return $fasterResponse->getBody();
}
```

Usage
-----

[](#usage)

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

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

```
Clue\React\Block\await(…);
```

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

```
use function Clue\React\Block\await;

await(…);
```

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

```
use Clue\React\Block;

Block\await(…);
```

### sleep()

[](#sleep)

The `sleep(float $seconds, ?LoopInterface $loop = null): void` function can be used to wait/sleep for `$time` seconds.

```
Clue\React\Block\sleep(1.5, $loop);
```

This function will only return after the given `$time` has elapsed. In the meantime, the event loop will run any other events attached to the same loop until the timer fires. If there are no other events attached to this loop, it will behave similar to the built-in [`sleep()`](https://www.php.net/manual/en/function.sleep.php).

Internally, the `$time` 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.

This function takes an optional `LoopInterface|null $loop` parameter that can be used to pass the event loop instance to use. You can use a `null` value here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). This value SHOULD NOT be given unless you're sure you want to explicitly use a given event loop instance.

Note that this function will assume control over the event loop. Internally, it will actually `run()` the loop until the timer fires and then calls `stop()` to terminate execution of the loop. This means this function is more suited for short-lived program executions when using async APIs is not feasible. For long-running applications, using event-driven APIs by leveraging timers is usually preferable.

### await()

[](#await)

The `await(PromiseInterface $promise, ?LoopInterface $loop = null, ?float $timeout = null): mixed` function can be used to block waiting for the given `$promise` to be fulfilled.

```
$result = Clue\React\Block\await($promise);
```

This function will only return after the given `$promise` has settled, i.e. either fulfilled or rejected. In the meantime, the event loop will run any events attached to the same loop until the promise settles.

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`, then this function will throw an `UnexpectedValueException` instead.

```
try {
    $result = Clue\React\Block\await($promise);
    // promise successfully fulfilled with $result
    echo 'Result: ' . $result;
} catch (Exception $exception) {
    // promise rejected with $exception
    echo 'ERROR: ' . $exception->getMessage();
}
```

See also the [examples](examples/).

This function takes an optional `LoopInterface|null $loop` parameter that can be used to pass the event loop instance to use. You can use a `null` value here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). This value SHOULD NOT be given unless you're sure you want to explicitly use a given event loop instance.

If no `$timeout` argument is given and the promise stays pending, then this will potentially wait/block forever until the promise is settled. To avoid this, API authors creating promises are expected to provide means to configure a timeout for the promise instead. For more details, see also the [`timeout()` function](https://github.com/reactphp/promise-timer#timeout).

If the deprecated `$timeout` argument is given and the promise is still pending once the timeout triggers, this will `cancel()` the promise and throw a `TimeoutException`. 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.

Note that this function will assume control over the event loop. Internally, it will actually `run()` the loop until the promise settles and then calls `stop()` to terminate execution of the loop. This means this function is more suited for short-lived promise executions when using promise-based APIs is not feasible. For long-running applications, using promise-based APIs by leveraging chained `then()` calls is usually preferable.

### awaitAny()

[](#awaitany)

The `awaitAny(PromiseInterface[] $promises, ?LoopInterface $loop = null, ?float $timeout = null): mixed` function can be used to wait for ANY of the given promises to be fulfilled.

```
$promises = array(
    $promise1,
    $promise2
);

$firstResult = Clue\React\Block\awaitAny($promises);

echo 'First result: ' . $firstResult;
```

See also the [examples](examples/).

This function will only return after ANY of the given `$promises` has been fulfilled or will throw when ALL of them have been rejected. In the meantime, the event loop will run any events attached to the same loop.

Once ANY promise is fulfilled, this function will return whatever this promise resolved to and will try to `cancel()` all remaining promises.

Once ALL promises reject, this function will fail and throw an `UnderflowException`. Likewise, this will throw if an empty array of `$promises` is passed.

This function takes an optional `LoopInterface|null $loop` parameter that can be used to pass the event loop instance to use. You can use a `null` value here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). This value SHOULD NOT be given unless you're sure you want to explicitly use a given event loop instance.

If no `$timeout` argument is given and ALL promises stay pending, then this will potentially wait/block forever until the promise is fulfilled. To avoid this, API authors creating promises are expected to provide means to configure a timeout for the promise instead. For more details, see also the [`timeout()` function](https://github.com/reactphp/promise-timer#timeout).

If the deprecated `$timeout` argument is given and ANY promises are still pending once the timeout triggers, this will `cancel()` all pending promises and throw a `TimeoutException`. 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.

Note that this function will assume control over the event loop. Internally, it will actually `run()` the loop until the promise settles and then calls `stop()` to terminate execution of the loop. This means this function is more suited for short-lived promise executions when using promise-based APIs is not feasible. For long-running applications, using promise-based APIs by leveraging chained `then()` calls is usually preferable.

### awaitAll()

[](#awaitall)

The `awaitAll(PromiseInterface[] $promises, ?LoopInterface $loop = null, ?float $timeout = null): mixed[]` function can be used to wait for ALL of the given promises to be fulfilled.

```
$promises = array(
    $promise1,
    $promise2
);

$allResults = Clue\React\Block\awaitAll($promises);

echo 'First promise resolved with: ' . $allResults[0];
```

See also the [examples](examples/).

This function will only return after ALL of the given `$promises` have been fulfilled or will throw when ANY of them have been rejected. In the meantime, the event loop will run any events attached to the same loop.

Once ALL promises are fulfilled, this will return an array with whatever each promise resolves to. Array keys will be left intact, i.e. they can be used to correlate the return array to the promises passed. Likewise, this will return an empty array if an empty array of `$promises` is passed.

Once ANY promise rejects, this will try to `cancel()` all remaining promises and throw an `Exception`. If the promise did not reject with an `Exception`, then this function will throw an `UnexpectedValueException` instead.

This function takes an optional `LoopInterface|null $loop` parameter that can be used to pass the event loop instance to use. You can use a `null` value here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). This value SHOULD NOT be given unless you're sure you want to explicitly use a given event loop instance.

If no `$timeout` argument is given and ANY promises stay pending, then this will potentially wait/block forever until the promise is fulfilled. To avoid this, API authors creating promises are expected to provide means to configure a timeout for the promise instead. For more details, see also the [`timeout()` function](https://github.com/reactphp/promise-timer#timeout).

If the deprecated `$timeout` argument is given and ANY promises are still pending once the timeout triggers, this will `cancel()` all pending promises and throw a `TimeoutException`. 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.

Note that this function will assume control over the event loop. Internally, it will actually `run()` the loop until the promise settles and then calls `stop()` to terminate execution of the loop. This means this function is more suited for short-lived promise executions when using promise-based APIs is not feasible. For long-running applications, using promise-based APIs by leveraging chained `then()` calls is usually preferable.

Install
-------

[](#install)

The recommended way to install this library is [through Composer](https://getcomposer.org/). [New to Composer?](https://getcomposer.org/doc/00-intro.md)

This project follows [SemVer](https://semver.org/). This will install the latest supported version:

```
$ composer require clue/block-react:^1.5
```

See also the [CHANGELOG](CHANGELOG.md) 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.3 through current PHP 8+ and HHVM. It's *highly recommended to use the latest supported PHP version* for this project.

Tests
-----

[](#tests)

To run the test suite, you first need to clone this repo and then install all dependencies [through Composer](https://getcomposer.org/):

```
$ composer install
```

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

```
$ vendor/bin/phpunit
```

License
-------

[](#license)

This project is released under the permissive [MIT license](LICENSE).

> Did you know that I offer custom development services and issuing invoices for sponsorships of releases and for contributions? Contact me (@clue) for details.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance43

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity32

Early-stage or recently created project

 Bus Factor1

Top contributor holds 75.5% 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 ~0 days

Total

2

Last Release

451d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/004321c516c7da4b4da19e4c71fa522bf0a8c82687786e7caac0c7f9aa5a7301?d=identicon)[korot1245](/maintainers/korot1245)

---

Top Contributors

[![clue](https://avatars.githubusercontent.com/u/776829?v=4)](https://github.com/clue "clue (77 commits)")[![SimonFrings](https://avatars.githubusercontent.com/u/44357440?v=4)](https://github.com/SimonFrings "SimonFrings (17 commits)")[![seregazhuk](https://avatars.githubusercontent.com/u/9959761?v=4)](https://github.com/seregazhuk "seregazhuk (2 commits)")[![andreybolonin](https://avatars.githubusercontent.com/u/2576509?v=4)](https://github.com/andreybolonin "andreybolonin (1 commits)")[![NikoGrano](https://avatars.githubusercontent.com/u/4658966?v=4)](https://github.com/NikoGrano "NikoGrano (1 commits)")[![PaulRotmann](https://avatars.githubusercontent.com/u/85174210?v=4)](https://github.com/PaulRotmann "PaulRotmann (1 commits)")[![Furgas](https://avatars.githubusercontent.com/u/715407?v=4)](https://github.com/Furgas "Furgas (1 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (1 commits)")[![davidcole1340](https://avatars.githubusercontent.com/u/991872?v=4)](https://github.com/davidcole1340 "davidcole1340 (1 commits)")

---

Tags

event-loopasyncpromisereactphpsleepblockingsynchronousawait

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/korot1245-block-react/health.svg)

```
[![Health](https://phpackages.com/badges/korot1245-block-react/health.svg)](https://phpackages.com/packages/korot1245-block-react)
```

###  Alternatives

[react/promise-timer

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

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

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP

1.3k116.9M402](/packages/react-socket)[react/dns

Async DNS resolver for ReactPHP

536114.1M100](/packages/react-dns)[react/promise-stream

The missing link between Promise-land and Stream-land for ReactPHP

11512.9M45](/packages/react-promise-stream)[react/async

Async utilities and fibers for ReactPHP

2228.8M171](/packages/react-async)[clue/docker-react

Async, event-driven access to the Docker Engine API, built on top of ReactPHP.

113154.9k1](/packages/clue-docker-react)

PHPackages © 2026

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