Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise - start - await: output order

Is the order of the values returned by await always relative to the input order?

my @aoa = (
    qw<1 a>, qw<2 b>, qw<3 c>,
    qw<4 d>, qw<5 e>, qw<6 f>,
    qw<7 g>, qw<8 h>, qw<9 i>,
);

my @portions = ( ( 0, 2 ), ( 3, 5 ), ( 6, 8 ) );
my @promise;
for @portions -> $range {
    @promise.push: start {
        do for $range[0] .. $range[1] -> $row {
            do for ^@aoa.[$row] -> $col {
                my $str = @aoa[$row][$col] // '';
                $row, $col, $str;
            }
        }
    };
}
for await @promise -> @portion {
    for @portion -> @p_rows {
        say @p_rows.join( ', ' );
    }
}
like image 843
sid_com Avatar asked Dec 05 '18 07:12

sid_com


1 Answers

Yes, the slurpy form of await is explicitly designed so that one can do things like:

my ($spec, $config) = await start { load-yaml slurp $spec-file },
                            start { from-json slurp $config-file };

And have the correct things assigned, without regard to which completes first. In the case in the question - pushing Promises onto an array - then the result order will match the order of those Promises, even if a latter one completes sooner in time.

like image 54
Jonathan Worthington Avatar answered Oct 23 '22 06:10

Jonathan Worthington