| 1 | package Promise::ES6::Future; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =encoding utf-8 | ||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Promise::ES6::Future - Translation to/from L<Future> | ||||||||||
| 11 | |||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||
| 13 | |||||||||||
| 14 | This module provides logic to convert between | ||||||||||
| 15 | promises and L<Future> instances. | ||||||||||
| 16 | |||||||||||
| 17 | =head1 FUNCTIONS | ||||||||||
| 18 | |||||||||||
| 19 | =head1 $promise = from_future( $FUTURE ) | ||||||||||
| 20 | |||||||||||
| 21 | Returns a L<Promise:ES6> instance from the given $FUTURE. | ||||||||||
| 22 | (If $FUTURE is not a L<Future> instance, $FUTURE is returned.) | ||||||||||
| 23 | |||||||||||
| 24 | =cut | ||||||||||
| 25 | |||||||||||
| 26 | 1 | ▶ | sub from_future { | ||||||||
|
Subroutine
from_future: called CC=2 SCAR=10.9
Pod
undocumented
| |||||||||||
| 27 | 1 | my ($whatsit) = @_; | |||||||||
| 28 | |||||||||||
| 29 | 1 | ▶ | return $whatsit if !$whatsit->isa('Future'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | |||||||||||
| 31 | 0 | local ($@, $!); | |||||||||
| 32 | 0 | require Promise::ES6; | |||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | return Promise::ES6->new( sub { () = $whatsit->then(@_) } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | =head1 $future = to_future( $PROMISE ) | ||||||||||
| 38 | |||||||||||
| 39 | Returns a L<Future> 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<Promise::ES6> instances. In fact, anything that I<isn’t> a Future | ||||||||||
| 44 | instance will cause this function to create a new Future. | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | sub to_future { | ||||||||
|
Subroutine
to_future: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 49 | 0 | my ($whatsit) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | return $whatsit if $whatsit->isa('Future'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 0 | local ($@, $!); | |||||||||
| 54 | 0 | require Future; | |||||||||
| 55 | |||||||||||
| 56 | 0 | my $future = Future->new(); | |||||||||
| 57 | |||||||||||
| 58 | $whatsit->then( | ||||||||||
| 59 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 60 | 0 | $future->done( $_[0] ); | |||||||||
| 61 | }, | ||||||||||
| 62 | 0 | ▶ | sub { $future->fail( $_[0] ) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 0 | ); | |||||||||
| 64 | |||||||||||
| 65 | 0 | return $future; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | 1; | |||||||||