PHPackages                             leocavalcante/swoole-futures - 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. leocavalcante/swoole-futures

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

leocavalcante/swoole-futures
============================

Futures + async/await for PHP's Swoole concurrency run-time.

v0.1.0(6y ago)1032.5k↓15.4%3MITPHPPHP &gt;=7.4CI failing

Since Feb 29Pushed 6y ago6 watchersCompare

[ Source](https://github.com/leocavalcante/swoole-futures)[ Packagist](https://packagist.org/packages/leocavalcante/swoole-futures)[ RSS](/packages/leocavalcante-swoole-futures/feed)WikiDiscussions master Synced 1mo ago

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

Swoole Futures
==============

[](#swoole-futures)

[![https://github.com/leocavalcante/swoole-futures/actions?query=workflow%3ACI](https://github.com/leocavalcante/swoole-futures/workflows/CI/badge.svg)](https://github.com/leocavalcante/swoole-futures/workflows/CI/badge.svg)[![https://shepherd.dev/github/leocavalcante/swoole-futures](https://camo.githubusercontent.com/13d6ea0b7c34b74e7da8c718658a16ba80ccacb73d73cb3cf05bb99afa8a0c74/68747470733a2f2f73686570686572642e6465762f6769746875622f6c656f636176616c63616e74652f73776f6f6c652d667574757265732f636f7665726167652e737667)](https://camo.githubusercontent.com/13d6ea0b7c34b74e7da8c718658a16ba80ccacb73d73cb3cf05bb99afa8a0c74/68747470733a2f2f73686570686572642e6465762f6769746875622f6c656f636176616c63616e74652f73776f6f6c652d667574757265732f636f7665726167652e737667)

⏳ Futures, Streams &amp; Async/Await for PHP's [Swoole](https://www.swoole.co.uk/) asynchronous run-time.

> Inspired by [futures Crate](https://crates.io/crates/futures) for Rust's Tokio asynchronous run-time.

It's on top of [Swoole's coroutines system](https://www.swoole.co.uk/coroutine) there is no special wizardry, just sugar.

Install
-------

[](#install)

```
composer require leocavalcante/swoole-futures
```

Usage
-----

[](#usage)

- [Basic / Hello, world!](#async--await)
- [Join](#join)
- [Race / Select](#race)
- [Async map](#async-map)
- [Then](#then)
- [Stream](#stream)

### Async / await

[](#async--await)

Creates and awaits for asynchronous computations in an alternative style than Swoole's coroutines.

```
$future = Futures\async(fn() => 1);
$result = $future->await(); // 1
```

Futures are lazy, it only runs when you call `await`.

### Join

[](#join)

Joins a list of Futures into a single Future that awaits for a list of results.

```
$slow_rand = function (): int {
    Co::sleep(3);
    return rand(1, 100);
};

$n1 = Futures\async($slow_rand);
$n2 = Futures\async($slow_rand);
$n3 = Futures\async($slow_rand);

$n = Futures\join([$n1, $n2, $n3]);

print_r($n->await());
```

This takes 3 seconds, not 9, Futures runs concurrently! (Order isn't guaranteed)

### Race

[](#race)

Returns the result of the first finished Future.

```
use Swoole\Coroutine\Http\Client;

$site1 = Futures\async(function() {
    $client = new Client('www.google.com', 443, true);
    $client->get('/');
    return $client->body;
});

$site2 = Futures\async(function() {
    $client = new Client('www.swoole.co.uk', 443, true);
    $client->get('/');
    return $client->body;
});

$site3 = Futures\async(function() {
    $client = new Client('leocavalcante.dev', 443, true);
    $client->get('/');
    return $client->body;
});

$first_to_load = Futures\race([$site1, $site2, $site3]);

echo $first_to_load;
```

And there is a `Futures\select` alias.

### Async map

[](#async-map)

Maps an array into a list of Futures where which item runs concurrently.

```
$list = [1, 2, 3];
$multiply = fn(int $a) => fn(int $b) => $a * $b;
$double = $multiply(2);

$doubles = Futures\join(Futures\async_map($list, $double))->await();

print_r($doubles);
```

### Then

[](#then)

Sequences a series of steps for a Future, is the serial analog for `join`:

```
use function Futures\async;

$future = async(fn() => 2)
    ->then(fn(int $i) => async(fn() => $i + 3))
    ->then(fn(int $i) => async(fn() => $i * 4))
    ->then(fn(int $i) => async(fn() => $i - 5));

echo $future->await(); // 15
```

### Stream

[](#stream)

Streams values/events from `sink` to `listen` with between operations.

```
$stream = Futures\stream()
    ->map(fn($val) => $val + 1)
    ->filter(fn($val) => $val % 2 === 0)
    ->map(fn($val) => $val * 2)
    ->listen(fn($val) => print("$val\n")); // 4 8 12 16 20

foreach (range(0, 9) as $n) {
    $stream->sink($n);
}
```

---

MIT © 2020

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

2266d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/940717934113da7abe00589e87cfde6f34496f039bcd3fc6ce4a33f5f415d4ac?d=identicon)[leocavalcante](/maintainers/leocavalcante)

---

Top Contributors

[![leocavalcante](https://avatars.githubusercontent.com/u/183722?v=4)](https://github.com/leocavalcante "leocavalcante (30 commits)")

---

Tags

async-awaitconcurrencyfuturesphpstreamsswooleswoole-coroutines

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/leocavalcante-swoole-futures/health.svg)

```
[![Health](https://phpackages.com/badges/leocavalcante-swoole-futures/health.svg)](https://phpackages.com/packages/leocavalcante-swoole-futures)
```

###  Alternatives

[jenner/array_group_by

like mysql group by

438.0k](/packages/jenner-array-group-by)

PHPackages © 2026

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