#!perlusestrict;usewarnings;useMojo::UserAgent;useMojo::IOLoop;useMojo::Base-strict;{packageMojo::UserAgent::Promises;usestrict;usewarnings;usePromisesqw[ deferred ];useMojo::Base'Mojo::UserAgent';useMojo::Util'monkey_patch';substart{my($self,$tx,$cb)=@_;my$d=deferred;$self->SUPER::start($tx,sub{$d->resolve(@_)});return$d->then($cb)if$cb;return$d->promise;}formy$name(qw(DELETE GET HEAD OPTIONS PATCH POST PUT)){monkey_patch__PACKAGE__,sprintf("a%s",lc($name)),sub{my$self=shift;my$method=lc($name);$self->$method(@_);};}}my$ua=Mojo::UserAgent::Promises->new;my$delay=Mojo::IOLoop->delay;my@steps=();my@titles=();foreachmy$url(qw[ mojolicio.us www.cpan.org ]){push(@steps,sub{my$end=$delay->begin;$ua->aget($url)->then(sub{my($promise,$tx)=@_;my$title=$tx->res->dom->at('title')->text;push(@titles,$title);$end->();})->then(sub{say("Joy");});});}$delay->steps(@steps)->wait;printjoin"\n"=>@titles;print"\n";1;
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.