|
4 | 4 |
|
5 | 5 | use React;
|
6 | 6 | use React\EventLoop\Loop;
|
| 7 | +use React\Promise\Deferred; |
7 | 8 | use React\Promise\Promise;
|
8 | 9 | use function React\Async\async;
|
9 | 10 | use function React\Async\await;
|
@@ -84,6 +85,49 @@ public function testAsyncReturnsPendingPromiseWhenCallbackReturnsPendingPromise(
|
84 | 85 | $promise->then($this->expectCallableNever(), $this->expectCallableNever());
|
85 | 86 | }
|
86 | 87 |
|
| 88 | + public function testAsyncWithAwaitReturnsReturnsPromiseFulfilledWithValueImmediatelyWhenPromiseIsFulfilled() |
| 89 | + { |
| 90 | + $deferred = new Deferred(); |
| 91 | + |
| 92 | + $promise = async(function () use ($deferred) { |
| 93 | + return await($deferred->promise()); |
| 94 | + })(); |
| 95 | + |
| 96 | + $return = null; |
| 97 | + $promise->then(function ($value) use (&$return) { |
| 98 | + $return = $value; |
| 99 | + }); |
| 100 | + |
| 101 | + $this->assertNull($return); |
| 102 | + |
| 103 | + $deferred->resolve(42); |
| 104 | + |
| 105 | + $this->assertEquals(42, $return); |
| 106 | + } |
| 107 | + |
| 108 | + public function testAsyncWithAwaitReturnsPromiseRejectedWithExceptionImmediatelyWhenPromiseIsRejected() |
| 109 | + { |
| 110 | + $deferred = new Deferred(); |
| 111 | + |
| 112 | + $promise = async(function () use ($deferred) { |
| 113 | + return await($deferred->promise()); |
| 114 | + })(); |
| 115 | + |
| 116 | + $exception = null; |
| 117 | + $promise->then(null, function ($reason) use (&$exception) { |
| 118 | + $exception = $reason; |
| 119 | + }); |
| 120 | + |
| 121 | + $this->assertNull($exception); |
| 122 | + |
| 123 | + $deferred->reject(new \RuntimeException('Test', 42)); |
| 124 | + |
| 125 | + $this->assertInstanceof(\RuntimeException::class, $exception); |
| 126 | + assert($exception instanceof \RuntimeException); |
| 127 | + $this->assertEquals('Test', $exception->getMessage()); |
| 128 | + $this->assertEquals(42, $exception->getCode()); |
| 129 | + } |
| 130 | + |
87 | 131 | public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsAfterAwaitingPromise()
|
88 | 132 | {
|
89 | 133 | $promise = async(function () {
|
|
0 commit comments