File Coverage

blib/lib/Future/Role/Promisify.pm
Criterion Covered Total %
statement 14 14 100.0
branch 3 4 75.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 23 24 95.8


line stmt bran cond sub pod time code
1             package Future::Role::Promisify;
2              
3 3     3   1366 use Mojo::Promise;
  3         7  
  3         50  
4 3     3   91 use Role::Tiny;
  3         6  
  3         16  
5              
6             our $VERSION = '1.002';
7              
8             requires qw(on_done on_fail retain);
9              
10             sub promisify {
11 6     6 1 124 my ($self, $p) = @_;
12 6 100       16 unless (defined $p) {
13 5         20 $p = Mojo::Promise->new;
14 5 50       187 $p->ioloop($self->loop) if $self->isa('Future::Mojo');
15             }
16 4     4   302351 $self->on_done(sub { $p->resolve(@_) })
17 6     2   68 ->on_fail(sub { $p->reject(@_) })->retain;
  2         100842  
18 6         613 return $p;
19             }
20              
21             1;
22              
23             =head1 NAME
24              
25             Future::Role::Promisify - Chain a Mojo::Promise from a Future
26              
27             =head1 SYNOPSIS
28              
29             use IO::Async::Loop::Mojo;
30             use Role::Tiny ();
31              
32             my $loop = IO::Async::Loop::Mojo->new;
33             my $future = $loop->timeout_future(after => 5);
34             Role::Tiny->apply_roles_to_object($future, 'Future::Role::Promisify');
35             $future->promisify->then(sub { say 'Resolved' })->catch(sub { warn 'Rejected' })->wait;
36              
37             use Future::Mojo;
38             use Mojo::IOLoop;
39              
40             my $loop = Mojo::IOLoop->new;
41             my $future = Future::Mojo->new($loop);
42             $loop->timer(1 => sub { $future->done('Success!') });
43             $future->promisify->then(sub { say @_ })->wait;
44              
45             =head1 DESCRIPTION
46              
47             L provides an interface to chain L
48             objects from L objects.
49              
50             =head1 METHODS
51              
52             L composes the following methods.
53              
54             =head2 promisify
55              
56             my $promise = $future->promisify;
57              
58             Returns a L object that will resolve or reject when the
59             L becomes ready. It will be assigned the L of the
60             Future if it is an instance of L.
61              
62             If the Future is not immediately ready or an instance of L, it
63             must be an instance of a Future that uses the L singleton, such
64             as an L from the L loop. In any other
65             circumstances, the resulting L may not be able to settle the
66             Future.
67              
68             If a promise object is passed, it will be used and returned instead of
69             constructing a new L. It may be an object of any class that has
70             a standard Promises/A+ API (in particular C and C methods),
71             but you are responsible for ensuring that it uses the correct event loop to
72             settle the Future if needed.
73              
74             $promise = $future->promisify($promise);
75              
76             =head1 CAVEATS
77              
78             Cancelling the preceding Future chain may lead to unspecified behavior.
79              
80             =head1 BUGS
81              
82             Report any issues on the public bugtracker.
83              
84             =head1 AUTHOR
85              
86             Dan Book
87              
88             =head1 COPYRIGHT AND LICENSE
89              
90             This software is Copyright (c) 2019 by Dan Book.
91              
92             This is free software, licensed under:
93              
94             The Artistic License 2.0 (GPL Compatible)
95              
96             =head1 SEE ALSO
97              
98             L, L, L