Skip to content

Commit

Permalink
Merge pull request reactphp#30 from clue-labs/the-future-is-now
Browse files Browse the repository at this point in the history
Improve `async()` to avoid unneeded `futureTick()` calls
  • Loading branch information
WyriHaximus authored Feb 18, 2022
2 parents 83749dd + ce2379f commit 1986075
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function async(callable $function): callable
}
});

Loop::futureTick(static fn() => $fiber->start());
$fiber->start();
});
}

Expand Down
59 changes: 50 additions & 9 deletions tests/AsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,35 @@
use function React\Async\async;
use function React\Async\await;
use function React\Promise\all;
use function React\Promise\reject;
use function React\Promise\resolve;

class AsyncTest extends TestCase
{
public function testAsyncReturnsPendingPromise()
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsValue()
{
$promise = async(function () {
return 42;
})();

$promise->then($this->expectCallableNever(), $this->expectCallableNever());
$value = null;
$promise->then(function ($v) use (&$value) {
$value = $v;
});

$this->assertEquals(42, $value);
}

public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns()
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsPromiseThatFulfillsWithValue()
{
$promise = async(function () {
return 42;
return resolve(42);
})();

$value = await($promise);
$value = null;
$promise->then(function ($v) use (&$value) {
$value = $v;
});

$this->assertEquals(42, $value);
}
Expand All @@ -37,10 +47,41 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow
throw new \RuntimeException('Foo', 42);
})();

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Foo');
$this->expectExceptionCode(42);
await($promise);
$exception = null;
$promise->then(null, function ($reason) use (&$exception) {
$exception = $reason;
});

assert($exception instanceof \RuntimeException);
$this->assertInstanceOf(\RuntimeException::class, $exception);
$this->assertEquals('Foo', $exception->getMessage());
$this->assertEquals(42, $exception->getCode());
}

public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackReturnsPromiseThatRejectsWithException()
{
$promise = async(function () {
return reject(new \RuntimeException('Foo', 42));
})();

$exception = null;
$promise->then(null, function ($reason) use (&$exception) {
$exception = $reason;
});

assert($exception instanceof \RuntimeException);
$this->assertInstanceOf(\RuntimeException::class, $exception);
$this->assertEquals('Foo', $exception->getMessage());
$this->assertEquals(42, $exception->getCode());
}

public function testAsyncReturnsPendingPromiseWhenCallbackReturnsPendingPromise()
{
$promise = async(function () {
return new Promise(function () { });
})();

$promise->then($this->expectCallableNever(), $this->expectCallableNever());
}

public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsAfterAwaitingPromise()
Expand Down

0 comments on commit 1986075

Please sign in to comment.