13 $promise->then(
function($value) use (&$finalValue) {
14 $finalValue = $value + 2;
18 $this->assertEquals(3, $finalValue);
28 $promise->then(null,
function($value) use (&$finalValue) {
29 $finalValue = $value + 2;
33 $this->assertEquals(3, $finalValue);
43 $promise->then(
function($value) use (&$finalValue) {
44 $finalValue = $value + 2;
46 })->then(
function($value) use (&$finalValue) {
47 $finalValue = $value + 4;
52 $this->assertEquals(7, $finalValue);
63 $promise->then(
function($value) use ($subPromise) {
65 })->then(
function($value) use (&$finalValue) {
66 $finalValue = $value + 4;
70 $subPromise->fulfill(2);
73 $this->assertEquals(6, $finalValue);
82 $promise->then(
function($value) use (&$finalValue) {
83 $finalValue = $value + 2;
89 $this->assertEquals(6, $finalValue);
98 $promise->then(null,
function($value) use (&$finalValue) {
99 $finalValue = $value + 2;
105 $this->assertEquals(6, $finalValue);
115 }))->then(
function(
$result) use (&$realResult) {
122 $this->assertEquals(
'hi', $realResult);
132 }))->then(
function(
$result) use (&$realResult) {
134 $realResult =
'incorrect';
136 },
function($reason) use (&$realResult) {
138 $realResult = $reason;
143 $this->assertEquals(
'hi', $realResult);
173 $promise->otherwise(
function($reason) {
175 $this->assertEquals(
'foo', $reason);
176 throw new \Exception(
'hi');
178 })->then(
function() use (&
$ok) {
182 },
function() use (&
$ok) {
188 $this->assertEquals(0,
$ok);
192 $this->assertEquals(1,
$ok);
202 Promise::all([$promise1, $promise2])->then(
function($value) use (&$finalValue) {
204 $finalValue = $value;
208 $promise1->fulfill(1);
210 $this->assertEquals(0, $finalValue);
212 $promise2->fulfill(2);
214 $this->assertEquals([1, 2], $finalValue);
225 function($value) use (&$finalValue) {
229 function($value) use (&$finalValue) {
230 $finalValue = $value;
234 $promise1->reject(1);
236 $this->assertEquals(1, $finalValue);
237 $promise2->reject(2);
239 $this->assertEquals(1, $finalValue);
250 function($value) use (&$finalValue) {
254 function($value) use (&$finalValue) {
255 $finalValue = $value;
259 $promise1->reject(1);
261 $this->assertEquals(1, $finalValue);
262 $promise2->fulfill(2);
264 $this->assertEquals(1, $finalValue);
275 function($value) use (&$finalValue) {
276 $finalValue = $value;
278 function($value) use (&$finalValue) {
279 $finalValue = $value;
283 $promise1->fulfill(1);
285 $this->assertEquals(1, $finalValue);
286 $promise2->fulfill(2);
288 $this->assertEquals(1, $finalValue);
299 function($value) use (&$finalValue) {
300 $finalValue = $value;
302 function($value) use (&$finalValue) {
303 $finalValue = $value;
307 $promise1->reject(1);
309 $this->assertEquals(1, $finalValue);
310 $promise2->reject(2);
312 $this->assertEquals(1, $finalValue);
343 $promise->reject(
new \OutOfBoundsException(
'foo'));
347 $this->fail(
'We did not get the expected exception');
349 $this->assertInstanceOf(
'OutOfBoundsException', $e);
350 $this->assertEquals(
'foo', $e->getMessage());
363 $this->fail(
'We did not get the expected exception');
365 $this->assertInstanceOf(
'Exception', $e);
366 $this->assertEquals(
'foo', $e->getMessage());
379 $this->fail(
'We did not get the expected exception');
381 $this->assertInstanceOf(
'Exception', $e);
382 $this->assertEquals(
'Promise was rejected with reason of type: array', $e->getMessage());
An implementation of the Promise pattern.
$promise
This example shows demonstrates the Promise api.
testAllRejectThenResolve()
testWaitWillNeverResolve()
static all(array $promises)
Deprecated.
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
testWaitRejectedException()
race(array $promises)
The race function returns a promise that resolves or rejects as soon as one of the promises in the ar...
testWaitRejectedNonScalar()