Modify response from a hook? #2184
-
I need to modify the response of a certain controller action, let's say At first I thought of something like this: $app->hook(before_render => sub {
my ($c, $args) = @_;
my $action = $c->match->stack->[-1]{action};
return unless ref $c eq 'App::Controller::Foo' and $action eq 'bar';
my $custom_data = 'hello world';
$args->{json}{custom_data} = $custom_data;
}); However in my case $app->hook(before_render => sub {
my ($c, $args) = @_;
my $action = $c->match->stack->[-1]{action};
return unless ref $c eq 'App::Controller::Foo' and $action eq 'bar';
# need to wait for this before rendering stuff
my $p = retrieve_custom_data->then(sub {
my $custom_data = shift;
$args->{json}{custom_data} = $custom_data;
})->catch(\&handle_errors);
}); which means I need to delay rendering until It's not like I can just call Anything I can do here? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You wouldn't be able to cause the hook to wait for the result except by blocking. Only the around_action hook is powerful enough to dictate when the dispatch continues. |
Beta Was this translation helpful? Give feedback.
-
I'm sure there are legitimate needs for a non-blocking before_render hook, but in your case, checking the controller and action, couldn't you just append your hook code to your action? actions that return a promise are waited on by the loop |
Beta Was this translation helpful? Give feedback.
You wouldn't be able to cause the hook to wait for the result except by blocking. Only the around_action hook is powerful enough to dictate when the dispatch continues.