Skip to content

Commit 1986075

Browse files
authored
Merge pull request #30 from clue-labs/the-future-is-now
Improve `async()` to avoid unneeded `futureTick()` calls
2 parents 83749dd + ce2379f commit 1986075

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function async(callable $function): callable
164164
}
165165
});
166166

167-
Loop::futureTick(static fn() => $fiber->start());
167+
$fiber->start();
168168
});
169169
}
170170

tests/AsyncTest.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,35 @@
88
use function React\Async\async;
99
use function React\Async\await;
1010
use function React\Promise\all;
11+
use function React\Promise\reject;
12+
use function React\Promise\resolve;
1113

1214
class AsyncTest extends TestCase
1315
{
14-
public function testAsyncReturnsPendingPromise()
16+
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsValue()
1517
{
1618
$promise = async(function () {
1719
return 42;
1820
})();
1921

20-
$promise->then($this->expectCallableNever(), $this->expectCallableNever());
22+
$value = null;
23+
$promise->then(function ($v) use (&$value) {
24+
$value = $v;
25+
});
26+
27+
$this->assertEquals(42, $value);
2128
}
2229

23-
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns()
30+
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsPromiseThatFulfillsWithValue()
2431
{
2532
$promise = async(function () {
26-
return 42;
33+
return resolve(42);
2734
})();
2835

29-
$value = await($promise);
36+
$value = null;
37+
$promise->then(function ($v) use (&$value) {
38+
$value = $v;
39+
});
3040

3141
$this->assertEquals(42, $value);
3242
}
@@ -37,10 +47,41 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow
3747
throw new \RuntimeException('Foo', 42);
3848
})();
3949

40-
$this->expectException(\RuntimeException::class);
41-
$this->expectExceptionMessage('Foo');
42-
$this->expectExceptionCode(42);
43-
await($promise);
50+
$exception = null;
51+
$promise->then(null, function ($reason) use (&$exception) {
52+
$exception = $reason;
53+
});
54+
55+
assert($exception instanceof \RuntimeException);
56+
$this->assertInstanceOf(\RuntimeException::class, $exception);
57+
$this->assertEquals('Foo', $exception->getMessage());
58+
$this->assertEquals(42, $exception->getCode());
59+
}
60+
61+
public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackReturnsPromiseThatRejectsWithException()
62+
{
63+
$promise = async(function () {
64+
return reject(new \RuntimeException('Foo', 42));
65+
})();
66+
67+
$exception = null;
68+
$promise->then(null, function ($reason) use (&$exception) {
69+
$exception = $reason;
70+
});
71+
72+
assert($exception instanceof \RuntimeException);
73+
$this->assertInstanceOf(\RuntimeException::class, $exception);
74+
$this->assertEquals('Foo', $exception->getMessage());
75+
$this->assertEquals(42, $exception->getCode());
76+
}
77+
78+
public function testAsyncReturnsPendingPromiseWhenCallbackReturnsPendingPromise()
79+
{
80+
$promise = async(function () {
81+
return new Promise(function () { });
82+
})();
83+
84+
$promise->then($this->expectCallableNever(), $this->expectCallableNever());
4485
}
4586

4687
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsAfterAwaitingPromise()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy