Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active September 8, 2016 06:36
Show Gist options
  • Save jberger/3607b149db20258e25a52b7596eb9d23 to your computer and use it in GitHub Desktop.
Save jberger/3607b149db20258e25a52b7596eb9d23 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
helper iodelay => sub {
my $cb = pop;
my ($c, @steps) = @_;
Mojo::IOLoop->delay(
@steps,
sub { $c->$cb(undef, @_) }
)->catch(sub{ $c->$cb($_[1]) });
};
any '/' => sub {
my $c = shift;
$c->iodelay(
sub{
my $delay = shift;
$delay->pass(text => 'hello world');
Mojo::IOLoop->timer(1 => $delay->begin);
},
sub {
my ($delay, $err, @args) = @_;
$c->reply->exception($err) if $err;
$c->render(@args);
},
);
};
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment