ReactPHP Turns 11: Announcing Promise v3.0.0 — More Power to Asynchronous PHP!

Infinitypaul
4 min readJul 11, 2023

--

Photo by Becky Fantham on Unsplash

Well, well, well, ReactPHP, aren’t we growing up fast! It’s hard to believe it’s been 11 fantastic, action-packed years since we first met. Today, they are celebrating in style, marking the milestone with a fantastic gift to their community — the much-anticipated Promise v3.0.0! — A Gift that Keeps on Giving. So, let’s roll up our sleeves and dig into this incredible birthday bonanza, shall we?

The Delights Inside the Promise v3.0.0 Magic Box

The ReactPHP team delivered exactly what was promised. The freshly baked Promise v3.0.0, which has just been released, is jam-packed with improvements that will improve your entire experience. Here is a list of the headliners who will probably inspire you to party:

No More Hide and Seek with Errors

Do you remember back in the day when a bug would hide and seek inside a promise callback, leaving you baffled and dazed? Well, those days are history, thanks to Promise v3. No more puzzling over what could have gone wrong. Unhandled promise rejections now come with their own spotlight, popping up as error messages. Bug hunts just got a whole lot easier!

// Unhandled promise rejection with RuntimeException: Unhandled in example.php:2
reject(new RuntimeException('Unhandled'));

But the team didn’t stop there. To sprinkle more control over your debugging endeavors, they’ve added a swanky new

set_rejection_handler() function

Now that’s some extra icing on the cake!

Type Safety: Double-Checked and Good to Go

Welcome to a world where type safety is the new norm. Promise v3 comes with native PHP type declarations neatly knitted across APIs, ensuring promises are as safe as houses. This upgrade is a boon not just for IDEs and static analysis tools, but also for the most sophisticated tool in your arsenal — your brain!

And there’s more! PHPStan template types are now part of the deal, adding an extra layer of safety. Now, isn’t that a fun two-way street? ReactPHP helps PHPStan run quickly, and PHPStan helps ReactPHP use PHPStan for quick development.

/** @return PromiseInterface<int> */
function answer(): PromiseInterface
{
return resolve(42);
}

// PHPStan now detects the following line as invalid
answer()->then(function (bool $result): string {
return 'Can you also tell what is wrong?';
});

Out with the Old, In with the New!

The launch of Promise v3.0.0 gave the team a chance to give their APIs a much-needed makeover. They’re now more in tune with the ES6 Promise specification you’d find in JavaScript. But a word of caution — this does entail a few minor BC breaks.

// old (arguments used to be optional)
$promise = resolve();
$promise = reject();

// new (already supported before)
$promise = resolve(null);
$promise = reject(new RuntimeException());

What’s more, for the sake of simplicity, the CancellablePromiseInterface and ExtendedPromiseInterface have been combined into a single PromiseInterface. Plus, courtesy of the updated PHP language support, methods like catch() and finally() are now part of the PromiseInterface.

// old (multiple interfaces may or may not be implemented)
assert($promise instanceof PromiseInterface);
assert(method_exists($promise, 'then'));
if ($promise instanceof ExtendedPromiseInterface) {
assert(method_exists($promise, 'otherwise'));
assert(method_exists($promise, 'always'));
}
if ($promise instanceof CancellablePromiseInterface) {
assert(method_exists($promise, 'cancel'));
}

// new (single PromiseInterface with all methods)
assert($promise instanceof PromiseInterface);
assert(method_exists($promise, 'then'));
assert(method_exists($promise, 'catch'));
assert(method_exists($promise, 'finally'));
assert(method_exists($promise, 'cancel'));

Now, for those of you wanting to deep dive into the intricacies of these changes, the Promise v3.0.0 changelog is your best buddy. And there’s also a shiny new reactphp/reactphp v1.4.0 meta package in town, installing all stable components, including the improved Promise v3.

The Road Ahead with Promise v3

With Promise v3 now strutting its stuff, the team is one giant leap closer to ReactPHP v3. This new version embodies the ambitious, electrifying future ReactPHP is headed towards.

The team has poured over 7 years into refining this component, prepping it for battle and priming it for production. They’ve pledged to extend their long-term support (LTS) for at least 24 months, promising (pun intended!) a sturdy foundation for your projects.

We can’t help but overflow with excitement for the experiences that lay ahead as we reflect on the 11 years’ worth of ups and downs. So let’s send birthday wishes to ReactPHP! Here’s to many more whirlwind years of asynchronous PHP with ReactPHP! 🥳

One Last Thing: The Changelog Peek: https://github.com/reactphp/reactphp/blob/1.x/CHANGELOG.md : With v3, this package is cruising into the future, while they continue to actively support v2 and v1 to ensure everyone enjoys a smooth sail. This update carries major new features and a minor BC break over the v2.0.0 release. They’ve bent over backward to minimize BC breaks and their impact. So, most users should be able to upgrade quicker than you can say ‘coffee break’!

For a full rundown of the changes, be sure to check out the official release notes.

https://clue.engineering/2023/announcing-reactphp-promise-v3

--

--

Infinitypaul
Infinitypaul

Written by Infinitypaul

Software Developer — I admire breaking up complex problems into understandable and sizeable bits, solving daily challenges with technology