Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Table of Contents
* [Deferred](#deferred)
* [Promise](#promise-1)
3. [API](#api)
* [DeferredInterface](#deferredinterface)
* [DeferredInterface::promise()](#deferredinterfacepromise)
* [DeferredInterface::resolve()](#deferredinterfaceresolve)
* [DeferredInterface::reject()](#deferredinterfacereject)
* [Deferred](#deferred-1)
* [Deferred::promise()](#deferredpromise)
* [Deferred::resolve()](#deferredresolve)
* [Deferred::reject()](#deferredreject)
* [PromiseInterface](#promiseinterface)
* [PromiseInterface::then()](#promiseinterfacethen)
* [PromiseInterface::catch()](#promiseinterfacecatch)
Expand Down Expand Up @@ -75,28 +76,17 @@ a placeholder for its actual result.
API
---

### Deferred
### DeferredInterface

The deferred interface provides the common interface for all deferred
implementations.
See [Deferred](#deferred-1) for the only public implementation exposed by this
package.

A deferred represents an operation whose resolution is pending. It has separate
promise and resolver parts.

```php
$deferred = new React\Promise\Deferred();

$promise = $deferred->promise();

$deferred->resolve(mixed $value);
$deferred->reject(\Throwable $reason);
```

The `promise` method returns the promise of the deferred.

The `resolve` and `reject` methods control the state of the deferred.

The constructor of the `Deferred` accepts an optional `$canceller` argument.
See [Promise](#promise-2) for more information.

#### Deferred::promise()
#### DeferredInterface::promise()

```php
$promise = $deferred->promise();
Expand All @@ -105,7 +95,7 @@ $promise = $deferred->promise();
Returns the promise of the deferred, which you can hand out to others while
keeping the authority to modify its state to yourself.

#### Deferred::resolve()
#### DeferredInterface::resolve()

```php
$deferred->resolve(mixed $value);
Expand All @@ -120,7 +110,7 @@ this promise once it is resolved.

See also the [`resolve()` function](#resolve).

#### Deferred::reject()
#### DeferredInterface::reject()

```php
$deferred->reject(\Throwable $reason);
Expand All @@ -133,6 +123,26 @@ All consumers are notified by having `$onRejected` (which they registered via

See also the [`reject()` function](#reject).

### Deferred

Creates a Deferred to control the state of an associated promise.

```php
$deferred = new React\Promise\Deferred();

$promise = $deferred->promise();

$deferred->resolve(mixed $value);
$deferred->reject(\Throwable $reason);
```

The `promise` method returns the promise of the deferred.

The `resolve` and `reject` methods control the state of the deferred.

The constructor of the `Deferred` accepts an optional `$canceller` argument.
See [Promise](#promise-2) for more information.

### PromiseInterface

The promise interface provides the common interface for all promise
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
<ini name="assert.active" value="1" />
<ini name="assert.exception" value="1" />
<ini name="assert.bail" value="0" />
<!-- Prevent Xdebug from overriding var_dump() -->
<env name="XDEBUG_MODE" value="off"/>
</php>
</phpunit>
3 changes: 2 additions & 1 deletion src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

/**
* @template T
* @template-implements DeferredInterface<T>
*/
final class Deferred
final class Deferred implements DeferredInterface
{
/**
* @var PromiseInterface<T>
Expand Down
23 changes: 23 additions & 0 deletions src/DeferredInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace React\Promise;

/**
* @template T
*/
interface DeferredInterface
{

/**
* @return PromiseInterface<T>
*/
public function promise(): PromiseInterface;

/**
* @param T $value
*/
public function resolve($value): void;

public function reject(\Throwable $reason): void;

}
19 changes: 16 additions & 3 deletions tests/FunctionResolveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,31 @@ function ($val) {
/** @test */
public function shouldSupportVeryDeepNestedPromises(): void
{
if (PHP_VERSION_ID < 70200 && ini_get('xdebug.max_nesting_level') !== false) {
$stackDepthLimit = ini_get('xdebug.max_nesting_level');
if (PHP_VERSION_ID < 70200 && $stackDepthLimit !== false) {
$this->markTestSkipped('Skip unhandled rejection on legacy PHP 7.1');
}

// Calculate the maximum number of nested promises we can handle
$stackOverhead = 25; // allow for PHPUnit's stack entries
$promiseStackRequirement = 4; // each nested promise requires 4 entries on the stack

// Normalise stack depth limit and ensure we have enough room on the stack for a meaningful test
$stackDepthLimit = (int) ($stackDepthLimit ?: 512);
if ($stackDepthLimit <= $stackOverhead + ($promiseStackRequirement * 10)) {
$this->markTestSkipped("Skip xdebug.max_nesting_limit set too low");
}

$nestingLimit = (int) (($stackDepthLimit - $stackOverhead) / $promiseStackRequirement);

$deferreds = [];

for ($i = 0; $i < 150; $i++) {
for ($i = 0; $i < $nestingLimit; $i++) {
$deferreds[] = $d = new Deferred();
$p = $d->promise();

$last = $p;
for ($j = 0; $j < 150; $j++) {
for ($j = 0; $j < $nestingLimit; $j++) {
$last = $last->then(function ($result) {
return $result;
});
Expand Down