Acme-CPANModulesBundle-Import-MojoliciousAdvent-2018
view release on metacpan or search on metacpan
devdata/https_mojolicious.io_blog_2018_12_03_higher-order-promises_ view on Meta::CPAN
sub { say "None of them worked!" },
);
$some_promise->wait;
</code></pre>
<h3>None</h3>
<p>A "none" Promise resolves when all of the its Promises are rejected. It's a trivial case that might be useful somewhere and I created it mostly because Perl 6 has a <a href="https://docs.perl6.org/routine/none">none Junction</a> (whi...
<p>For this very simple example, consider the task to check that no sites are that annoying "404 File Not Found":</p>
<pre><code>use v5.28;
use utf8;
use strict;
use warnings;
use feature qw(signatures);
no warnings qw(experimental::signatures);
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
devdata/https_mojolicious.io_blog_2018_12_03_higher-order-promises_ view on Meta::CPAN
my @urls = qw(
https://www.learning-perl.com/
https://www.perl.org/
https://perldoc.perl.org/not_there.pod
);
my @all_sites = map {
my $p = $ua->get_p( $_ );
$p->then( sub ( $tx ) {
$tx->res->code == 404 ? $tx->req->url : die $tx->req->url
} );
} @urls;
my $all_promise = Mojo::Promise
->with_roles( '+None' )
->none( @all_sites )
->then(
sub { say "None of them were 404!" },
sub { say "At least one was 404: @_!" },
);
$all_promise->wait;
</code></pre>
<h2>Conclusion</h2>
<p>It's easy to make new Promises out of smaller ones to represent complex situations. You can combine the Promises that Mojolicious creates for you with your own handmade Promises to do almost anything you like.</p>
</section>
( run in 2.253 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )