Skip to content

Instantly share code, notes, and snippets.

@jonathanstowe
Created June 8, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanstowe/a93e637ef971e7eac92a to your computer and use it in GitHub Desktop.
Save jonathanstowe/a93e637ef971e7eac92a to your computer and use it in GitHub Desktop.
Async gather
(
gather {
my $c = Channel.new;
my @ps = do for ^10 { start { sleep rand; $c.send($_) } };
my $p = Promise.allof(@ps);
loop {
earliest $c {
more * {
take $_;
}
};
last if $p;
}
await @ps;
}).map(*.say)
@smls
Copy link

smls commented Jun 8, 2015

(
    gather {
        my $c = Channel.new;
        my @ps = do for ^10 { start { sleep rand; $c.send($_) } };
        Promise.allof(@ps).then({ $c.close });
        take $_ for $c.list;
    }
).map(*.say)

@jonathanstowe
Copy link
Author

That's much nicer :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment