Skip Menu |

This queue is for tickets about the future CPAN distribution.

Report information
The Basics
Id: 106959
Status: rejected
Priority: 0/
Queue: future

People
Owner: Nobody in particular
Requestors: ALTREUS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: ->finally ?
I have a use case where I don't really care whether a Future failed or not; I just want the result. The sample code below tries to report on the results of the forked processes, which ->fail if there was a nonzero exit code. As a result, I have to extract the data from the Future first. A ->finally method could perform this job, i.e. to ignore the actual error in a failed Future, and simply run the code with the rest of the failure data as the return value of the operation itself. -- use App::Multigit qw(mg_each); use curry; sub extract_future { my $f = shift; my @result; if ($f->failure) { (undef, @result) = $f->failure; } else { @result = $f->get; } Future->done(@result); } mg_each(sub{ my $repo = shift; $repo->run([qw/ this command might fail /]) ->followed_by(\&extract_future) ->then($repo->curry::report) }); -- use App::Multigit qw/mg_each/; use curry; mg_each(sub{ my $repo = shift; $repo->run([qw/ this command might fail /]) ->finally($repo->curry::report) });
On 2015-09-08 17:21:34, ALTREUS wrote: Show quoted text
> I have a use case where I don't really care whether a Future failed or > not; I just want the result. > > The sample code below tries to report on the results of the forked > processes, which ->fail if there was a nonzero exit code. As a result, > I have to extract the data from the Future first. > > A ->finally method could perform this job, i.e. to ignore the actual > error in a failed Future, and simply run the code with the rest of the > failure data as the return value of the operation itself. > > -- > > use App::Multigit qw(mg_each); > use curry; > > sub extract_future { > my $f = shift; > > my @result; > > if ($f->failure) { > (undef, @result) = $f->failure; > } > else { > @result = $f->get; > } > > Future->done(@result); > } > > mg_each(sub{ > my $repo = shift; > > $repo->run([qw/ this command might fail /]) > ->followed_by(\&extract_future) > ->then($repo->curry::report) > }); > > -- > > use App::Multigit qw/mg_each/; > use curry; > > mg_each(sub{ > my $repo = shift; > > $repo->run([qw/ this command might fail /]) > ->finally($repo->curry::report) > });
Sounds like: ->run(...) ->else(sub { Future->done(@_) }) ->then($repo->curry::report) although ->finally could be a useful wrapper - presumably it should always be called, even if the future was cancelled? Tom
On Sun Sep 13 17:29:43 2015, TEAM wrote: Show quoted text
> > Sounds like: > > ->run(...) > ->else(sub { Future->done(@_) }) > ->then($repo->curry::report) > > although ->finally could be a useful wrapper - presumably it should > always be called, even if the future was cancelled? > > Tom
That's right, only it would allow me to be more specific with the elses. The conclusion in IRC was that it is *also* part of my use case that the failures I'm throwing have the same shape of data as the successes; so in the general case of Future users it is not particularly useful to have a function that "unwraps" the Future irrespective of its type, being that failures will have different shapes from successes and from each other. I'll reject this myself, but good talk.