File Coverage

blib/lib/Promise/ES6/Future.pm
Criterion Covered Total %
statement 8 21 38.1
branch 1 4 25.0
condition n/a
subroutine 3 7 42.8
pod 0 2 0.0
total 12 34 35.2


line stmt bran cond sub pod time code
1             package Promise::ES6::Future;
2              
3 1     1   446 use strict;
  1         3  
  1         29  
4 1     1   4 use warnings;
  1         2  
  1         302  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             Promise::ES6::Future - Translation to/from L
11              
12             =head1 DESCRIPTION
13              
14             This module provides logic to convert between
15             promises and L instances.
16              
17             =head1 FUNCTIONS
18              
19             =head1 $promise = from_future( $FUTURE )
20              
21             Returns a L instance from the given $FUTURE.
22             (If $FUTURE is not a L instance, $FUTURE is returned.)
23              
24             =cut
25              
26             sub from_future {
27 1     1 0 7 my ($whatsit) = @_;
28              
29 1 50       13 return $whatsit if !$whatsit->isa('Future');
30              
31 0           local ($@, $!);
32 0           require Promise::ES6;
33              
34 0     0     return Promise::ES6->new( sub { () = $whatsit->then(@_) } );
  0            
35             }
36              
37             =head1 $future = to_future( $PROMISE )
38              
39             Returns a L instance from the given $PROMISE.
40             (If $PROMISE is a Future instance, $PROMISE is returned.)
41              
42             Note that this function can work with promise objects that aren’t
43             L instances. In fact, anything that I a Future
44             instance will cause this function to create a new Future.
45              
46             =cut
47              
48             sub to_future {
49 0     0 0   my ($whatsit) = @_;
50              
51 0 0         return $whatsit if $whatsit->isa('Future');
52              
53 0           local ($@, $!);
54 0           require Future;
55              
56 0           my $future = Future->new();
57              
58             $whatsit->then(
59             sub {
60 0     0     $future->done( $_[0] );
61             },
62 0     0     sub { $future->fail( $_[0] ) },
63 0           );
64              
65 0           return $future;
66             }
67              
68             1;