Skip to content

Commit b67fea3

Browse files
committed
Fast forward resolved/rejected promises with await
This makes `await`ing an already resolved promise significantly faster. Ported from: #18
1 parent c989ee1 commit b67fea3

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/functions.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,41 @@
5353
function await(PromiseInterface $promise)
5454
{
5555
$wait = true;
56-
$resolved = null;
57-
$exception = null;
56+
$resolved = false;
5857
$rejected = false;
58+
$resolvedValue = null;
59+
$rejectedThrowable = null;
5960

6061
$promise->then(
61-
function ($c) use (&$resolved, &$wait) {
62-
$resolved = $c;
62+
function ($c) use (&$resolved, &$resolvedValue, &$wait) {
63+
$resolvedValue = $c;
64+
$resolved = true;
6365
$wait = false;
6466
Loop::stop();
6567
},
66-
function ($error) use (&$exception, &$rejected, &$wait) {
67-
$exception = $error;
68+
function ($error) use (&$rejected, &$rejectedThrowable, &$wait) {
69+
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
70+
if (!$error instanceof \Exception && !$error instanceof \Throwable) {
71+
$error = new \UnexpectedValueException(
72+
'Promise rejected with unexpected value of type ' . (is_object($error) ? get_class($error) : gettype($error))
73+
);
74+
}
75+
76+
$rejectedThrowable = $error;
6877
$rejected = true;
6978
$wait = false;
7079
Loop::stop();
7180
}
7281
);
7382

83+
if ($rejected) {
84+
throw $rejectedThrowable;
85+
}
86+
87+
if ($resolved) {
88+
return $resolvedValue;
89+
}
90+
7491
// Explicitly overwrite argument with null value. This ensure that this
7592
// argument does not show up in the stack trace in PHP 7+ only.
7693
$promise = null;
@@ -80,17 +97,10 @@ function ($error) use (&$exception, &$rejected, &$wait) {
8097
}
8198

8299
if ($rejected) {
83-
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
84-
if (!$exception instanceof \Throwable) {
85-
$exception = new \UnexpectedValueException(
86-
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception))
87-
);
88-
}
89-
90-
throw $exception;
100+
throw $rejectedThrowable;
91101
}
92102

93-
return $resolved;
103+
return $resolvedValue;
94104
}
95105

96106

tests/AwaitTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
122122
$this->assertEquals(0, gc_collect_cycles());
123123
}
124124

125+
public function testAlreadyFulfilledPromiseShouldNotSuspendFiber()
126+
{
127+
for ($i = 0; $i < 6; $i++) {
128+
$this->assertSame($i, React\Async\await(React\Promise\resolve($i)));
129+
}
130+
}
131+
125132
public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue()
126133
{
127134
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {

0 commit comments

Comments
 (0)