->wait will not block if the event loop is currently running, like if you
are setting this up in an event loop callback like a request handler. In
order to cause promises to block while running the main event loop, you
need to run the promises in a separate event loop.

my $promise = Mojo::Promise->new(ioloop => Mojo::IOLoop->new);

On Wed, Mar 14, 2018 at 10:06 AM, Michael Fung <[email protected]> wrote:

> Thanks Andre. I already have the "return $promise" statement to end the
> sub, so it is not the cause.
>
> I really suppose the construct will work like the recv function of
> AnyEvent->condvar.
>
>
> On Wednesday, March 14, 2018 at 9:11:53 PM UTC+8, Andre Parker wrote:
>>
>> Hi.
>>
>> You should return promise object in get_data sub. Something like:
>>
>> my $get_data = sub {
>>   my $promise = Mojo::Promise->new();
>>   # Some code that resolves promise
>>   ...
>>   return $promise;
>> };
>>
>> On Wednesday, 14 March 2018 12:18:07 UTC+2, Michael Fung wrote:
>>>
>>> Hi all,
>>>
>>> I have the following construct using Mojo::Promise:
>>>
>>> my $get_data =  sub {
>>>   create new Promise object;
>>>   read data from database
>>>   if data read ok {
>>>     $promise->resolve($data);
>>>   } else {
>>>     $promise->reject('no data');
>>>   }
>>> };
>>>
>>>
>>> $get_data->()
>>> ->then( sub {
>>>   # do something with data
>>>   ...
>>> })
>>> ->catch( sub {
>>>   # log error
>>>   ...
>>> })
>>> ->wait;
>>>
>>> say "the end";
>>>
>>> However, I got *"the end*" before running the "do something with data"
>>> block or catch block. Is there any way to make ->wait really wait?
>>>
>>>
>>> Thanks,
>>> Michael
>>>
>>>
>>>
>>>
>>>
>>> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to