Hi Ken,
Many thanks for your help again.
I think I have finally got it!
The code example is basically the same example as this AnyEvent example from Perl Maven:
https://perlmaven.com/fetching-several-web-pages-in-parallel-using-anyevent
The "condvar" in AnyEvent is basically the same "delay" in Mojo. I find it easier to understand in the context of AnyEvent that condvar "condition variable" represent a condition that must become true.
You'll notice the sequence and logic of the codes in both Perl Maven and Mojo cookbook are the same.
I am wondering if it is possible for the event-loop to finish (and exit) before it can start calling the inner function.
So as what you have found, the problem is related to the codes for loop control:
### my $end = $delay->begin;
...
### $end->();
I notice if I change the sequence of the $end->(); as following:
$fetch->();
$end->();
Instead of
$end->();
$fetch->();
The code will now process all the URLs one by one when it is only $fetch->().
And if I run it with for-loop like $fetch->() for 1 .. 2, it looks like processing 2 URLs in 1 go.
The code behaving much more like the original intended.
The problem with the original code is that if you are calling it 1 time only, at the time reaching the end of the event loop the condition may have satisfied and exit the program before it can call another function itself.(my guess)
(I hope the above phase are correct in describing the issue. This event loop is too new for me)