Last try

32 views
Skip to first unread message

Jan Eskilsson

unread,
Apr 12, 2018, 2:03:04 PM4/12/18
to mojol...@googlegroups.com

Hi All

The piece of code below always render "Test" indicating to me that the $mytest variable is never touched in the anonymous sub in the then or the catch. I cant understand what i do wrong ?

sub search {
    my $self = shift;
  
    my $mytest = 'Test';
    $self->stockitems->search_p('*')->then(sub {
      my $results = shift;      
     
      $mytest = 'Test Test Test';
    })->catch (sub {
      my $err = shift;
      
      $mytest = 'ERROR';
    })->wait;;

    $self->render(template => 'stockitems/search',  mytest => $mytest);
}

Any feedback or ideas are much apreciated

Thank you in advance !


Best Regards
Jan

--
Titles mean nothing.  The one with a servant's heart is the leader.
 
Please consider the environment before you print this email.
 
All incoming and outgoing emails and any attachments are subjected to a virus scanner and are believed to be free of any virus, or any other defect which might affect any computer or IT system into which they are received and opened. Therefore, it is the responsibility of the recipient to ensure that they are virus free and no responsibility is accepted by Jan Eskilsson  for any loss or damage arising in any way from receipt or use thereof.

Dan Book

unread,
Apr 12, 2018, 2:19:47 PM4/12/18
to mojol...@googlegroups.com
The wait method will only block until the promise completes if the event loop is not currently running. To be portable, anything that relies on the results of the promise should only occur in further promise callbacks. And note also that you can resolve promises using values by returning them from the callbacks, and those values will be passed to the next callback, for example:

$self->stockitems->search_p('*')->then(sub {
  ...
  return 'Test Test Test';
})->catch(sub {
  ...
  return 'ERROR';
})->then(sub {
  my $mytest = shift;
  $self->render(template => 'stockitems/search', mytest => $mytest);
})->wait;

-Dan

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

Reply all
Reply to author
Forward
0 new messages