Skip to content

does the React\Promise\all returns the results in order? #538

Answered by clue
Amir-adibi asked this question in Q&A
Discussion options

You must be logged in to vote

does the React\Promise\all returns the results in order?

Yes, the all() function is guaranteed to preserve the original array (or iterator) keys and their order.

<?php

require __DIR__ . '/vendor/autoload.php';

React\Promise\all([
    'first' => new React\Promise\Promise(function (callable $resolve): void {
        React\EventLoop\Loop::addTimer(0.2, fn() => $resolve(1));
    }),
    'second' => new React\Promise\Promise(function (callable $resolve): void {
        React\EventLoop\Loop::addTimer(0.1, fn() => $resolve(2));
    })
])->then(function (array $results): void {
    var_dump($results);
});
array(2) {
  'first' =>
  int(1)
  'second' =>
  int(2)
}

https://play.phpsandbox.io/re…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Amir-adibi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants