The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::UserAgent::Role::PromiseClass - Choose the Promise class used by Mojo::UserAgent

VERSION

version 0.004

SYNOPSIS

  $ua = Mojo::UserAgent->new(...)
        ->with_roles('+PromiseClass')
        ->promise_roles('+Repeat');  # add promise features you want

  $ua->get_p('http://example.com')
     ->repeat(sub{...});  # and they show up on every get_p call

DESCRIPTION

Mojo::UserAgent::Role::PromiseClass is a role that allows specifying the promise class to be used for the promise-returning methods like "get_p" in UserAgent and "post_p" in UserAgent, if you want something different from Mojo::Promise.

Note that since most methods on Mojo::Promise will use clone to create new instances, roles assigned in this way will usually propagate down method chains. (As of version 8.25, the only places in core Mojolicious other than Mojo::UserAgent where promises are being created from scratch is "proxy->start_p" in Mojolicious::Plugin::DefaultHelpers and related helpers, which you would need to wrap if your application uses them.)

ATTRIBUTES

Mojo::UserAgent::Role::PromiseClass implements the following attributes.

promise_class

  $pclass = $ua->promise_class;
  $ua     = $ua->promise_class('Mojo::Promise');

Specifies the class to use for promises returned by "start_p" in User::Agent and all derived routines ("get_p" in User::Agent, "post_p" in User::Agent, ...).

METHODS

Mojo::UserAgent::Role::PromiseClass supplies the following methods:

promise_roles

  $ua->promise_roles(@roles);

This is a shortcut to add the specified @roles to the user agent's promise_class, returning the original User::Agent, equivalent to

  $ua->promise_class($ua->promise_class->with_roles(@roles));

Using this method is slightly safer than setting "promise_class" directly in that if the user agent's existing promise_class is derived from Mojo::Promise (which it will be by default) then you won't be changing that, and this is typically what you want.

For roles following the naming scheme Mojo::Promise::Role::RoleName you can use the shorthand +RoleName.

SEE ALSO

Mojo::UserAgent, Mojo::Promise, Mojolicious, Mojolicious::Guides, https://mojolicious.org.

AUTHOR

Roger Crew <wrog@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by Roger Crew.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.