Skip to content

Perhaps there is an iterative approach instead of recursive? #22

Closed
@mtdowling

Description

@mtdowling

This library currently uses recursion for promise chain resolution. Using a promise and chaining off of the promise potentially forever, will cause a stack overflow due to deep recursion.

Here's a really contrived example:

$d = new \React\Promise\Deferred();
$p = $d->promise();
$f = function ($x) {
    echo xdebug_get_stack_depth() . ', ';
    return $x;
};

$last = $p;
for ($i = 0; $i < 1000; $i++) {
    $last = $last->then($f);
}

$d->resolve('test');

Running this code will show that the stack depth grows significantly for each promise resolution:

9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69, 74, 79, 84, 89, 94, PHP Fatal error:  Maximum function nesting level of '100' reached, aborting!

I don't usually use XDebug's default nesting limit of 100, but this illustrates the point that this library provides a recursive solution. I wonder if there is a way to implement promise resolution iteratively.

@jsor Have you ever attempted this or considered an iterative approach? It seems like some kind of directed acyclic graph structure that branches off of the deferred() would allow for an iterative approach.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions