[Mojolicious] iterated promises (Mojo::Promise::Role::Repeat)

2019-10-01 Thread Roger Crew
So I've uploaded Mojo::Promise::Role::Repeat to CPAN.

This adds a single method to Mojo::Promise
  

$done_p = $promise->repeat(sub{...})


which is essentially equivalent to

$done_p = $promise->then(sub{...})->then(sub{...})->then(sub{...})->then(sub
{...}) #... forever


The promise returned represents the end of the chain off at infinity, 
meaning

   - if any instance of the handler sub{...} dies or returns a rejected 
   promise
   $done_p is rejected with the same @reason(s).
   - if any instance of sub {...} calls $_->(@values) -- $_ is bound to an 
   escape function that does not return --
   $done_p is resolved with @values (and execution of everything else in 
   the loop is abandoned).

and these are the only ways out of the loop.  If a handler run returns 
normally or returns a resolved promise, the return values become the 
arguments for the next iteration (as one would expect for consecutive 
then()s).

The function bound to $_ also works from inside of nested handlers/loops 
(though you'll want to stash it in a lexical if you're going to do that 
since $_ will probably be set to something different by the time the nested 
handler runs).  Meaning you can do stuff like this

$ua->get_p(...)->repeat(sub{
   my $break = $_;
   ...
   $ua->get_p(...)->repeat(sub {
   ...
   $ua->get_p(...)->then(sub {
  ...
  $break->('bye') if (condition);
  ...
   }
   ...
   }
   ...
})


Anyway it's up.  Enjoy.

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/d171bb82-9662-4b35-b6bc-ce28a4507ff4%40googlegroups.com.


Re: [Mojolicious] iterated promises (Mojo::Promise::Role::Repeat)

2019-10-01 Thread Dan Book
You might look at the existing art of
https://metacpan.org/pod/Future::Utils#REPEATING-A-BLOCK-OF-CODE which can
be used with Mojo::Promise via futurify/promisify, and seems a lot clearer
to me.

-Dan

On Tue, Oct 1, 2019 at 9:04 AM Roger Crew  wrote:

> something I have found vaguely useful that I just uploaded to CPAN for
> people to play with
> Mojo::Promise::Role::Repeat,
>
> adds a *repeat()* method to the promise class/objects.
>
> The essential idea is that
>
> $promise->repeat(sub{...})
>
>
> is equivalent to
>
> $promise->then(sub{...})->then(sub{...})->then(sub{...})->then(sub{...}) #
>  forever
>
>
> except that repeat() also returns a "final" promise, which is
>
>- rejected, if any iteration dies with a value or returns a promise
>that gets rejected
>- resolved with @values, if any iteration calls $_->(@values)
>
> and if any run of the handler sub{...} returns normally or with a promise
> that gets resolved, those values get fed to the next run,
>
> $_ being bound, at the start of each run, to an escape function that
> doesn't return -- the moral equivalent of a break statement -- *but *you
> can also use this function to break out of nested loops/handlers as well
> (though you'll need to stash it in a lexical if you're going to do that).
>
> The pattern that seems to be coming up a lot for me is
>
> $ua->get_p($first_url)->repeat(sub {
>  # ... parse page ...
>  $_->() if (victory);
>  # ...
>  $ua->get_p($next_url);
> })
>
>
> (I may eventually want to petition for some form of this to be added to
> Mojo::Promise, but one thing at a time...)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mojolicious+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mojolicious/8ab45fa4-2b24-417a-8f57-4b99cfe82a4b%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/CABMkAVV3aB-JxtUPzB5-G3RrG81C9D33j%2B5_PdV14fxCBkaV-w%40mail.gmail.com.


[Mojolicious] iterated promises (Mojo::Promise::Role::Repeat)

2019-10-01 Thread Roger Crew
something I have found vaguely useful that I just uploaded to CPAN for 
people to play with
Mojo::Promise::Role::Repeat, 

adds a *repeat()* method to the promise class/objects.

The essential idea is that 

$promise->repeat(sub{...})


is equivalent to

$promise->then(sub{...})->then(sub{...})->then(sub{...})->then(sub{...}) # 
 forever


except that repeat() also returns a "final" promise, which is

   - rejected, if any iteration dies with a value or returns a promise that 
   gets rejected
   - resolved with @values, if any iteration calls $_->(@values)
   
and if any run of the handler sub{...} returns normally or with a promise 
that gets resolved, those values get fed to the next run,

$_ being bound, at the start of each run, to an escape function that 
doesn't return -- the moral equivalent of a break statement -- *but *you 
can also use this function to break out of nested loops/handlers as well 
(though you'll need to stash it in a lexical if you're going to do that).

The pattern that seems to be coming up a lot for me is

$ua->get_p($first_url)->repeat(sub {
 # ... parse page ...
 $_->() if (victory);
 # ... 
 $ua->get_p($next_url);
})


(I may eventually want to petition for some form of this to be added to 
Mojo::Promise, but one thing at a time...)

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/8ab45fa4-2b24-417a-8f57-4b99cfe82a4b%40googlegroups.com.