++ed by:

1 non-PAUSE user.

Jan Henning Thorsen

NAME

Mojo::Promisify - Convert callback code to promise based code

SYNOPSIS

  use Mojo::Promisify qw(promisify_call promisify_patch);
  use Some::NonBlockingClass;

  # Create an object from a callback based class
  my $nb_obj = Some::NonBlockingClass->new;

  # Call a callback based method, but return a Mojo::Promise
  promisify_call($nb_obj => get_stuff_by_id => 42)->then(sub {
    my @res = shift;
    warn @res;
  })->catch(sub {
    my $err = shift;
    die $err;
  });

  # Add a method that wraps around the callback based method and return a
  # Mojo::Promise.
  promisify_patch "Some::NonBlockingClass" => "get_stuff_by_id";

  # The added method has the "_p" suffix
  $nb_obj->get_stuff_by_id_p(42)->then(sub {
    my @res = shift;
    warn @res;
  });

DESCRIPTION

Mojo::Promisify is a utility module that can upgrade your legacy callback based API to a Mojo::Promise based API.

It might not be the most efficient way to run your code, but it will allow you to easily add methods that will return promises.

This method only works with methods that passes on $err as the first argument to the callback, like this:

  sub get_stuff_by_id {
    my ($self, $id, $cb) = @_;

    my $err = "Some error";
    my $res = undef;
    Mojo::IOLoop->next_tick(sub { $self->$cb($err, $res) });

    return $self;
  }

It can however pass on as many arguments as it wants after the $err and all will be passed on to the fulfillment callback in the promise. $err on the other hand will cause the promise to be rejected.

Note that this module is currently EXPERIMENTAL, but it will most probably not change much.

FUNCTIONS

promisify

  $code = promisify($obj => $method);
  $promise = $code->(@args);

Will return a curried function that wraps around a given $method in a $class and returns a promise. @args are the same arguments you would normally give to the $method, but without the callback at the end.

It can be useful to use this function instead of "promisify_call", in case you want to call the same $method on the same object over and over again.

promisify_call

  $promise = promisify_call($obj => $method, @args);

This function basically does:

  my $promise = promisify($obj => $method)->(@args);

promisify_patch

  promisify_patch $class, @methods;

Used to monkey patch a class with new promise based methods. The methods that are patched in, will have the "_p" suffix added.

Note that this function will replace existing methods!

AUTHOR

Jan Henning Thorsen

COPYRIGHT AND LICENSE

Copyright (C) 2019, Jan Henning Thorsen.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

Mojo::Promise