Skip Menu |

This queue is for tickets about the Future-AsyncAwait CPAN distribution.

Report information
The Basics
Id: 129223
Status: open
Priority: 0/
Queue: Future-AsyncAwait

People
Owner: Nobody in particular
Requestors: leonerd-cpan [...] leonerd.org.uk
Cc: DBOOK [...] cpan.org
AdminCc:

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



Subject: Consider an async {BLOCK} expression
There's a growing number of cases in both unit tests and real code, of writing $f = (async sub { ... })->(); This is often useful in e.g. cases involving Future->needs_any Future->needs_any( (async sub { # Real code goes here involving await expressions })->(), $loop->timeout_future(after => 10) ); These might look neater if there was some syntax sugar for doing that, Future->needs_any( async { # Real code goes here involving await expressions }, $loop->timeout_future(after => 10) ); As an implementation it would probably just desugar into the same notation above, though it could also set the CvNAME of the otherwise-anon sub inside, so that caller/die/croak/etc... will look a little neater -- Paul Evans
A possible added complication though: Since it doesn't look like a `sub`, where would return go? async sub outer { my $result = await async { return "OK"; }; } Does it make the outer() return, or does it set $result? By analogy with other constructs like Syntax::Keyword::Try, it would be nice if it made the entire outer function return, though arranging for that to happen might take more work. -- Paul Evans
But what about this potential rewrite? # previously use Future::Utils 'repeat'; sub example { return Future->wait_any( $loop->delay_future(after => 10) ->then_fail("Failed to get item in 10 seconds"), (repeat { return GET_ITEM() } until => sub { $_[0]->get->is_success }), ); } # becomes async sub example { await Future->wait_any( $loop->delay_future(after => 10) ->then_fail("Failed to get item in 10 seconds"), async { while(1) { my $result = await GET_ITEM(); return $result if $result->is_success; } } ); } -- Paul Evans
On Thu Apr 18 06:09:28 2019, PEVANS wrote: Show quoted text
> A possible added complication though: > > Since it doesn't look like a `sub`, where would return go? > > async sub outer > { > my $result = await async { > return "OK"; > }; > } > > Does it make the outer() return, or does it set $result? > > By analogy with other constructs like Syntax::Keyword::Try, it would > be nice if it made the entire outer function return, though arranging > for that to happen might take more work.
I think this construct is more just syntax sugar and shouldn't pretend it's not a subroutine as such. There are plenty of examples of blocks that act just like subroutines thanks to the & prototype.