kraih/mojo — pull request #655 by alexbyk (2014-07-26 02:38:39+00:00)
Title: Mojo::IOLoop::Delay jump method with 'labels' feature

Hello. I've wrote some feature. Could you take a look pls. Maybe it you'll find it worst to be commited to the Mojolicious.
Description:
Amazing and awesome feature (for awesome developers only). Everybody missed it.
Now you can avoid nested delays and stop doing mistakes by counting remaining steps. Here is an example:
```perl
use Mojo::Base -strict;
use Mojo::UserAgent;
use Mojo::IOLoop;

my $ua = Mojo::UserAgent->new->max_redirects(3);

# Try to download file from Github. If something wrong,
# jump to CPAN label and download from CPAN. If all is ok,
# (delete .NOT.EXISTING to see) jump to the latest step

my $delay = Mojo::IOLoop->delay(

sub {
$ua->get(
'https://raw.githubusercontent.com/kraih/mojo/master/Changes.NOT.EXISTING',
shift->begin
);
},
sub {
my ($d, $tx) = @_;

return $d->jump(label => 'CPAN') unless my $res = $tx->success;
$d->jump(index => -1, $res);
},
sub { warn "never happens" },

# labeled step
CPAN => sub {
$ua->get('http://api.metacpan.org/source/SRI/Mojolicious-5.18/Changes',
shift->begin);
},
sub {
my ($d, $tx) = @_;

# jump to the latest step
$d->jump(index => -1, $tx->success);
},

# some other staff
sub { warn 'this will never happen' },

# index => -1
sub {
my ($d, $res) = @_;

my $text = $res ? $res->body : "Nothing";

say substr($text, 0, 300);
$d->pass($text);
}

)->wait;
```