Skip to content

\React\Promise\all not preserving array key order #74

@lordthorzonus

Description

@lordthorzonus

Hi I ran into an strange issue and am wondering is this intended behavior?

Basically I'm expecting that for the included code the output would be:

.Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

But instead I'm getting the array keys unordered:

.Array
(
    [0] => 1
    [2] => 3
    [1] => 2
)

Which causes problems for example when json_encoding (PHP thinks it should be encoded as an object not an array).

Code to reproduce:

$loop = Factory::create();

$testData = [
    new Promise(
        function ($resolve) use ($loop) {
            $loop->nextTick(
                function () use ($resolve) {
                    $resolve(1);
                }
            );
        }
    ),
    new Promise(
        function ($resolve) use ($loop) {
            $loop->nextTick(
                function () use ($resolve, $loop) {
                    \React\Promise\resolve()->then( function() use ($resolve, $loop) {
                        $loop->nextTick(function () use ($resolve) {
                            $resolve(2);
                        });
                    });
                }
            );
        }
    ),
    new Promise(
        function ($resolve) use ($loop) {
            $loop->nextTick(
                function () use ($resolve) {
                    $resolve(3);
                }
            );
        }
    )
];


\React\Promise\all($testData)->then(function ($result) {
    print_r($result);
});

$loop->run();

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions