Skip to content

Commit

Permalink
make catch work properly for thenables
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 24, 2017
1 parent 1554997 commit 24d4fd2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop.pm
Expand Up @@ -453,7 +453,7 @@ Callbacks will be passed along to L<Mojo::IOLoop::Delay/"steps">.
say 'Never actually reached.';
}
)->catch(sub {
my ($delay, $err) = @_;
my $err = shift;
say "Something went wrong: $err";
})->wait;
Expand Down
16 changes: 11 additions & 5 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -15,6 +15,8 @@ sub begin {
return sub { $self->_step($id, $offset // 1, $len, @_) };
}

sub catch { shift->then(undef, shift) }

sub data { Mojo::Util::_stash(data => @_) }

sub pass { $_[0]->begin->(@_) }
Expand All @@ -39,10 +41,10 @@ sub then {
my ($self, $finish, $error) = @_;

my $next = $self->_clone;
$self->once(finish => sub { shift; $next->resolve(@_) });
$self->once(error => sub { shift; $next->reject(@_) });
$next->once(finish => sub { shift; $finish->(@_) }) if $finish;
$next->once(error => sub { shift; $error->(@_) }) if $error;
$self->on(finish => sub { shift; $next->resolve(@_) });
$self->on(error => sub { shift; $next->reject(@_) });
$next->on(finish => sub { shift; $finish->(@_) }) if $finish;
$next->on(error => sub { shift; $error->(@_) }) if $error;

return $next unless $self->{settled};

Expand Down Expand Up @@ -155,7 +157,7 @@ Mojo::IOLoop::Delay - Promises/A+ and flow-control helpers
say 'Never actually reached.';
}
)->catch(sub {
my ($delay, $err) = @_;
my $err = shift;
say "Something went wrong: $err";
})->wait;
Expand Down Expand Up @@ -320,6 +322,10 @@ together to the next step or L</"finish"> event.
Mojo::IOLoop->client({port => 4000} => $delay->begin);
$delay->wait;
=head2 catch
my $thenable = $delay->catch(sub {...});
=head2 data
my $hash = $delay->data;
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -81,7 +81,8 @@ sub _delay {
my $c = shift;
my $tx = $c->render_later->tx;
my $delay = Mojo::IOLoop->delay(@_);
$delay->catch(sub { $c->helpers->reply->exception(pop) and undef $tx })->wait;
$delay->catch(sub { $c->helpers->reply->exception(shift) and undef $tx })
->wait;
}

sub _development {
Expand Down Expand Up @@ -314,7 +315,7 @@ of the steps, breaking the chain.
$c->render_later;
my $tx = $c->tx;
my $delay = Mojo::IOLoop->delay(sub {...}, sub {...});
$delay->catch(sub { $c->reply->exception(pop) and undef $tx })->wait;
$delay->catch(sub { $c->reply->exception(shift) and undef $tx })->wait;
# Non-blocking request
$c->delay(
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/delay.t
Expand Up @@ -301,7 +301,7 @@ $delay->steps(
},
sub { die 'Second step!' },
sub { $result = 'failed' }
)->catch(sub { $failed = pop })->wait;
)->catch(sub { $failed = shift })->wait;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Second step!/, 'right error';
ok !$finished, 'finish event has not been emitted';
Expand Down

0 comments on commit 24d4fd2

Please sign in to comment.