Skip to content

Commit 57859f0

Browse files
committed
Forward compatibility with upcoming Promise v3
1 parent cfd52ac commit 57859f0

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
"require": {
2929
"php": ">=8.1",
3030
"react/event-loop": "^1.2",
31-
"react/promise": "^2.8 || ^1.2.1"
31+
"react/promise": "^3.0 || ^2.8 || ^1.2.1"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^9.3",
35-
"react/promise-timer": "^1.8"
35+
"react/promise-timer": "^1.9"
3636
},
3737
"autoload": {
3838
"psr-4": {

src/functions.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace React\Async;
44

5-
use React\EventLoop\Loop;
6-
use React\Promise\CancellablePromiseInterface;
75
use React\Promise\Deferred;
86
use React\Promise\Promise;
97
use React\Promise\PromiseInterface;
@@ -199,7 +197,7 @@ function async(callable $function): callable
199197
}, function () use (&$fiber): void {
200198
FiberMap::cancel($fiber);
201199
$promise = FiberMap::getPromise($fiber);
202-
if ($promise instanceof CancellablePromiseInterface) {
200+
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
203201
$promise->cancel();
204202
}
205203
});
@@ -277,7 +275,7 @@ function await(PromiseInterface $promise): mixed
277275
$rejectedThrowable = null;
278276
$lowLevelFiber = \Fiber::getCurrent();
279277

280-
if ($lowLevelFiber !== null && FiberMap::isCancelled($lowLevelFiber) && $promise instanceof CancellablePromiseInterface) {
278+
if ($lowLevelFiber !== null && FiberMap::isCancelled($lowLevelFiber) && $promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
281279
$promise->cancel();
282280
}
283281

@@ -486,7 +484,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
486484
$promise = null;
487485
$deferred = new Deferred(function () use (&$promise) {
488486
// cancel pending promise(s) as long as generator function keeps yielding
489-
while ($promise instanceof CancellablePromiseInterface) {
487+
while ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
490488
$temp = $promise;
491489
$promise = null;
492490
$temp->cancel();
@@ -541,7 +539,7 @@ function parallel(array $tasks): PromiseInterface
541539
$pending = [];
542540
$deferred = new Deferred(function () use (&$pending) {
543541
foreach ($pending as $promise) {
544-
if ($promise instanceof CancellablePromiseInterface) {
542+
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
545543
$promise->cancel();
546544
}
547545
}
@@ -560,7 +558,7 @@ function parallel(array $tasks): PromiseInterface
560558
$deferred->reject($error);
561559

562560
foreach ($pending as $promise) {
563-
if ($promise instanceof CancellablePromiseInterface) {
561+
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
564562
$promise->cancel();
565563
}
566564
}
@@ -598,7 +596,7 @@ function series(array $tasks): PromiseInterface
598596
{
599597
$pending = null;
600598
$deferred = new Deferred(function () use (&$pending) {
601-
if ($pending instanceof CancellablePromiseInterface) {
599+
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {
602600
$pending->cancel();
603601
}
604602
$pending = null;
@@ -638,7 +636,7 @@ function waterfall(array $tasks): PromiseInterface
638636
{
639637
$pending = null;
640638
$deferred = new Deferred(function () use (&$pending) {
641-
if ($pending instanceof CancellablePromiseInterface) {
639+
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {
642640
$pending->cancel();
643641
}
644642
$pending = null;

tests/SeriesTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testSeriesWillCancelFirstPendingPromiseWhenCallingCancelOnResult
8787
$tasks = array(
8888
function () {
8989
return new Promise(function ($resolve) {
90-
$resolve();
90+
$resolve(null);
9191
});
9292
},
9393
function () use (&$cancelled) {

tests/WaterfallTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testWaterfallWillCancelFirstPendingPromiseWhenCallingCancelOnRes
9494
$tasks = array(
9595
function () {
9696
return new Promise(function ($resolve) {
97-
$resolve();
97+
$resolve(null);
9898
});
9999
},
100100
function () use (&$cancelled) {

0 commit comments

Comments
 (0)