Skip to content

Commit

Permalink
deprecate use of Mojo::Promise::all and Mojo::Promise::race as instan…
Browse files Browse the repository at this point in the history
…ce methods
  • Loading branch information
kraih committed Jan 2, 2018
1 parent 5c4a9d0 commit 28a46cf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

7.60 2017-12-27
7.60 2018-01-02
- Deprecated use of Mojo::Promise::all and Mojo::Promise::race as instance
methods.
- Improved number detection in Mojo::JSON with a workaround for an upcoming
breaking change in Perl 5.28. (haarg)
- Improved HTML Living Standard compliance of Mojo::DOM::HTML.
Expand Down
38 changes: 24 additions & 14 deletions lib/Mojo/Promise.pm
Expand Up @@ -3,11 +3,17 @@ use Mojo::Base -base;

use Mojo::IOLoop;
use Scalar::Util qw(blessed weaken);
use Mojo::Util 'deprecated';

has ioloop => sub { Mojo::IOLoop->singleton };

sub all {
my ($class, @promises) = (ref $_[0] ? (undef, @_) : @_);
my ($class, @promises) = @_;

# DEPRECATED!
unshift(@promises, $class)
and deprecated 'Use of Mojo::Promise::all as instance method is DEPRECATED'
if ref $class;

my $all = $promises[0]->_clone;

Expand Down Expand Up @@ -41,10 +47,17 @@ sub finally {
}

sub race {
my ($class, @promises) = (ref $_[0] ? (undef, @_) : @_);
my $race = $promises[0]->_clone;
$_->then(sub { $race->resolve(@_) }, sub { $race->reject(@_) }) for @promises;
return $race;
my ($class, @promises) = @_;

# DEPRECATED!
unshift(@promises, $class)
and deprecated 'Use of Mojo::Promise::race as instance method is DEPRECATED'
if ref $class;

my $new = $promises[0]->_clone;
$_->then(sub { $new->resolve(@_) }, sub { $new->reject(@_) }) for @promises;

return $new;
}

sub reject { shift->_settle('reject', @_) }
Expand Down Expand Up @@ -202,14 +215,12 @@ the following new ones.
=head2 all
my $new = Mojo::Promise->all(@promises);
my $new = $promise->all(@promises);
Returns a new L<Mojo::Promise> object that either fulfills when all of the
passed L<Mojo::Promise> objects (including the invocant) have fulfilled or
rejects as soon as one of them rejects. If the returned promise fulfills, it is
fulfilled with the values from the fulfilled promises in the same order as the
passed promises. This method can be useful for aggregating results of multiple
promises.
passed L<Mojo::Promise> objects have fulfilled or rejects as soon as one of them
rejects. If the returned promise fulfills, it is fulfilled with the values from
the fulfilled promises in the same order as the passed promises. This method can
be useful for aggregating results of multiple promises.
=head2 catch
Expand Down Expand Up @@ -253,11 +264,10 @@ reason.
=head2 race
my $new = Mojo::Promise->race(@promises);
my $new = $promise->race(@promises);
Returns a new L<Mojo::Promise> object that fulfills or rejects as soon as one of
the passed L<Mojo::Promise> objects (including the invocant) fulfills or
rejects, with the value or reason from that promise.
the passed L<Mojo::Promise> objects fulfills or rejects, with the value or
reason from that promise.
=head2 reject
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -767,7 +767,7 @@ that have been bundled for internal use.
=head2 Mojolicious Artwork
Copyright (C) 2010-2017, Sebastian Riedel.
Copyright (C) 2010-2018, Sebastian Riedel.
Licensed under the CC-SA License, Version 4.0
L<http://creativecommons.org/licenses/by-sa/4.0>.
Expand Down Expand Up @@ -1141,7 +1141,7 @@ Zoffix Znet
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2008-2017, Sebastian Riedel and others.
Copyright (C) 2008-2018, Sebastian Riedel and others.
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/promise.t
Expand Up @@ -150,7 +150,7 @@ $promise = Mojo::Promise->new->then(sub {@_});
$promise2 = Mojo::Promise->new->then(sub {@_});
$promise3 = Mojo::Promise->new->then(sub {@_});
(@results, @errors) = ();
$promise->race($promise2, $promise3)
Mojo::Promise->race($promise, $promise2, $promise3)
->then(sub { @results = @_ }, sub { @errors = @_ });
$promise2->reject('second');
$promise3->resolve('third');
Expand All @@ -176,7 +176,7 @@ $promise = Mojo::Promise->new->then(sub {@_});
$promise2 = Mojo::Promise->new->then(sub {@_});
$promise3 = Mojo::Promise->new->then(sub {@_});
(@results, @errors) = ();
$promise->all($promise2, $promise3)
Mojo::Promise->all($promise, $promise2, $promise3)
->then(sub { @results = @_ }, sub { @errors = @_ });
$promise2->resolve('second');
$promise3->reject('third');
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/user_agent.t
Expand Up @@ -136,7 +136,7 @@ is $body, 'works!', 'right content';
my @results;
my $p1 = $ua->get_p('/');
my $p2 = $ua->get_p('/');
$p1->all($p2)->then(
Mojo::Promise->all($p1, $p2)->then(
sub {
my ($first, $second) = @_;
push @results, $first, $second;
Expand Down

0 comments on commit 28a46cf

Please sign in to comment.