Closed
Description
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.