Skip to content

Commit c30cebf

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 ab03f4d commit c30cebf

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/functions.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,27 @@
5151
function await(PromiseInterface $promise)
5252
{
5353
$wait = true;
54-
$resolved = null;
55-
$exception = null;
54+
$resolved = false;
5655
$rejected = false;
56+
$resolvedValue = null;
57+
$rejectedThrowable = null;
5758

5859
$promise->then(
59-
function ($c) use (&$resolved, &$wait) {
60-
$resolved = $c;
60+
function ($c) use (&$resolved, &$resolvedValue, &$wait) {
61+
$resolvedValue = $c;
62+
$resolved = true;
6163
$wait = false;
6264
Loop::stop();
6365
},
64-
function ($error) use (&$exception, &$rejected, &$wait) {
65-
$exception = $error;
66+
function ($error) use (&$rejected, &$rejectedThrowable, &$wait) {
67+
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
68+
if (!$error instanceof \Exception && !$error instanceof \Throwable) {
69+
$error = new \UnexpectedValueException(
70+
'Promise rejected with unexpected value of type ' . (is_object($error) ? get_class($error) : gettype($error))
71+
);
72+
}
73+
74+
$rejectedThrowable = $error;
6675
$rejected = true;
6776
$wait = false;
6877
Loop::stop();
@@ -73,22 +82,23 @@ function ($error) use (&$exception, &$rejected, &$wait) {
7382
// argument does not show up in the stack trace in PHP 7+ only.
7483
$promise = null;
7584

85+
if ($rejected) {
86+
throw $rejectedThrowable;
87+
}
88+
89+
if ($resolved) {
90+
return $resolvedValue;
91+
}
92+
7693
while ($wait) {
7794
Loop::run();
7895
}
7996

8097
if ($rejected) {
81-
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
82-
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
83-
$exception = new \UnexpectedValueException(
84-
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception))
85-
);
86-
}
87-
88-
throw $exception;
98+
throw $rejectedThrowable;
8999
}
90100

91-
return $resolved;
101+
return $resolvedValue;
92102
}
93103

94104
/**

tests/AwaitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
145145
$this->assertEquals(0, gc_collect_cycles());
146146
}
147147

148+
public function testAlreadyFulfilledPromiseShouldNotSuspendFiber()
149+
{
150+
for ($i = 0; $i < 6; $i++) {
151+
$this->assertSame($i, React\Async\await(React\Promise\resolve($i)));
152+
}
153+
}
154+
148155
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
149156
{
150157
if (method_exists($this, 'expectException')) {

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