1Promises::Sub(3)      User Contributed Perl Documentation     Promises::Sub(3)
2
3
4

NAME

6       Promises::Sub - Turns functions into promises
7

VERSION

9       version 1.04
10

SYNOPSIS

12           use Promises 'deferred';
13           use parent 'Promises::Sub';
14
15           sub shall_concat :Defer {
16               join ' ', @_;
17           }
18
19           my @promises = map { deferred } 1..2;
20
21           my @results = (
22               shall_concat( @promises ),
23               shall_concat( 'that is', $promises[1] ),
24               shall_concat( 'this is', 'straight up' ),
25           );
26
27           say "all results are promises";
28
29           $_->then(sub { say @_ } ) for @results;
30           # prints 'this is straight up'
31
32           say "two results are still waiting...";
33
34           $promises[1]->resolve( 'delayed' );
35           # prints 'this is delayed'
36
37           say "only one left...";
38
39           $promises[0]->resolve( 'finally the last one, that was' );
40           # prints 'finally the last one, that was delayed'
41

DESCRIPTION

43       Any function tagged with the ":Defer" will be turned into a promise, so
44       you can do
45
46           sub add :Defer { $_[0] + $_[1] }
47
48           add( 1,2 )->then(sub { say "the result is ", @_ } );
49
50       Additionally, if any of the arguments to the functions are promises
51       themselves, the function call will wait until those promises are
52       fulfilled before running.
53
54           my $number = deferred;
55
56           add( 1, $number )->then(sub { say "result: ", @_ } );
57
58           # $number is not fulfilled yet, nothing is printed
59
60           $number->resolve(47);
61           # prints 'result: 48'
62
63       Bear in mind that to use the ":Defer" attribute, you have to do "use
64       parent 'Promises::Sub';", and not "use Promises::Sub;" in the target
65       namespace.
66
67   Anonymous functions
68       The ":Defer" attribute won't work for anonymous functions and will
69       throw an exception. For those, you can export the function "defer",
70       which will wrap any coderef the same way that ":Defer" does.
71
72           use Promises::Sub qw/ defer /;
73
74           my $promised_sub = defer sub {
75               join ' ', @_;
76           };
77
78           my $p1 = deferred;
79
80           $promised_sub->( 'hello', $p1 )->then( sub {
81               say shift;
82           } );
83
84           # prints nothing
85
86           $p1->resolve('world');
87           # => prints 'hello world'
88

AUTHOR

90       Stevan Little <stevan.little@iinteractive.com>
91
93       This software is copyright (c) 2020, 2019, 2017, 2014, 2012 by Infinity
94       Interactive, Inc.
95
96       This is free software; you can redistribute it and/or modify it under
97       the same terms as the Perl 5 programming language system itself.
98
99
100
101perl v5.30.1                      2020-02-24                  Promises::Sub(3)
Impressum