Skip to content

Instantly share code, notes, and snippets.

@tommybutler
Last active July 9, 2021 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommybutler/57e8b6bc01c7672144069da13c7e424c to your computer and use it in GitHub Desktop.
Save tommybutler/57e8b6bc01c7672144069da13c7e424c to your computer and use it in GitHub Desktop.
Mojo Promise Map Concept
# inside my mojo route...
my $result = $library->run_object_method_that_runs_mojo_promise_map();
if ( Scalar::Util::blessed( $result ) && $result->isa( 'Mojo::Promise' ) )
{
$c->render_later();
my $start = scalar localtime;
return $result->then
(
sub
{
my $json =
{
start => $start,
finish => scalar localtime,
result => [ @_ ]
};
$c->respond_to( any => { json => $json, status => 200 } )
},
sub
{
my $json = { errors => [ @_ ] };
$c->respond_to( any => { json => $json, status => 500 } )
},
);
}
else
{
$c->respond_to( any => { json => $result, status => 200 } );
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment