NAME
Mojo::Promise::Role::Futurify - Chain a Future from a Mojo::Promise
SYNOPSIS
use Mojo::Promise;
my $promise = Mojo::Promise->with_roles('+Futurify')->new;
my $future = $promise->futurify->on_ready(sub {
my $f = shift;
say $f->is_done ? 'Done' : 'Failed';
});
$promise->ioloop->timer(5 => sub { $promise->resolve });
$future->await;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
# complicated way of doing $ua->get('https://example.com')
my $tx = $ua->get_p('https://example.com')->with_roles('+Futurify')->futurify->get;
# using Future composition methods
my @futures;
foreach my $url (@urls) {
push @futures, $ua->get_p($url)->with_roles('+Futurify')->futurify;
}
use Future;
Future->wait_all(@futures)->then(sub {
foreach my $f (@_) {
if ($f->is_done) {
my $tx = $f->get;
} elsif ($f->is_failed) {
my $err = $f->failure;
}
}
})->await;
DESCRIPTION
Mojo::Promise::Role::Futurify provides an interface to chain Future objects from Mojo::Promise objects.
METHODS
Mojo::Promise::Role::Futurify composes the following methods.
futurify
my $future = $promise->futurify;
Returns a Future::Mojo object that will become ready with success or failure when the Mojo::Promise resolves or rejects.
BUGS
Report any issues on the public bugtracker.
AUTHOR
Dan Book <dbook@cpan.org>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2017 by Dan Book.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)