/usr/share/perl5/Mojo/Promise.pm is in libmojolicious-perl 7.59+dfsg-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | package Mojo::Promise;
use Mojo::Base -base;
use Mojo::IOLoop;
use Scalar::Util qw(blessed weaken);
has ioloop => sub { Mojo::IOLoop->singleton };
sub all {
my ($class, @promises) = (ref $_[0] ? (undef, @_) : @_);
my $all = $promises[0]->_clone;
my $results = [];
my $remaining = scalar @promises;
for my $i (0 .. $#promises) {
$promises[$i]->then(
sub {
$results->[$i] = [@_];
$all->resolve(@$results) if --$remaining <= 0;
},
sub { $all->reject(@_) }
);
}
return $all;
}
sub catch { shift->then(undef, shift) }
sub finally {
my ($self, $finally) = @_;
my $new = $self->_clone;
push @{$self->{resolve}}, sub { _finally($new, $finally, 'resolve', @_) };
push @{$self->{reject}}, sub { _finally($new, $finally, 'reject', @_) };
$self->_defer if $self->{result};
return $new;
}
sub race {
my ($class, @promises) = (ref $_[0] ? (undef, @_) : @_);
my $race = $promises[0]->_clone;
$_->then(sub { $race->resolve(@_) }, sub { $race->reject(@_) }) for @promises;
return $race;
}
sub reject { shift->_settle('reject', @_) }
sub resolve { shift->_settle('resolve', @_) }
sub then {
my ($self, $resolve, $reject) = @_;
my $new = $self->_clone;
push @{$self->{resolve}}, sub { _then($new, $resolve, 'resolve', @_) };
push @{$self->{reject}}, sub { _then($new, $reject, 'reject', @_) };
$self->_defer if $self->{result};
return $new;
}
sub wait {
my $self = shift;
return if (my $loop = $self->ioloop)->is_running;
$self->finally(sub { $loop->stop });
$loop->start;
}
sub _clone {
my $self = shift;
my $clone = $self->new;
weaken $clone->ioloop($self->ioloop)->{ioloop};
return $clone;
}
sub _defer {
my $self = shift;
return unless my $result = $self->{result};
my $cbs = $self->{status} eq 'resolve' ? $self->{resolve} : $self->{reject};
@{$self}{qw(resolve reject)} = ([], []);
$self->ioloop->next_tick(sub { $_->(@$result) for @$cbs });
}
sub _finally {
my ($new, $finally, $method, @result) = @_;
my ($res) = eval { $finally->(@result) };
return $new->$method(@result)
unless $res && blessed $res && $res->can('then');
$res->then(sub { $new->$method(@result) }, sub { $new->$method(@result) });
}
sub _settle {
my ($self, $status) = (shift, shift);
return $self if $self->{result};
@{$self}{qw(result status)} = ([@_], $status);
$self->_defer;
return $self;
}
sub _then {
my ($new, $cb, $method, @result) = @_;
return $new->$method(@result) unless defined $cb;
my @res;
return $new->reject($@) unless eval { @res = $cb->(@result); 1 };
return $new->resolve(@res)
unless @res == 1 && blessed $res[0] && $res[0]->can('then');
$res[0]->then(sub { $new->resolve(@_); () }, sub { $new->reject(@_); () });
}
1;
=encoding utf8
=head1 NAME
Mojo::Promise - Promises/A+
=head1 SYNOPSIS
use Mojo::Promise;
use Mojo::UserAgent;
# Wrap continuation-passing style APIs with promises
my $ua = Mojo::UserAgent->new;
sub get {
my $promise = Mojo::Promise->new;
$ua->get(@_ => sub {
my ($ua, $tx) = @_;
my $err = $tx->error;
$promise->resolve($tx) if !$err || $err->{code};
$promise->reject($err->{message});
});
return $promise;
}
# Perform non-blocking operations sequentially
get('http://mojolicious.org')->then(sub {
my $mojo = shift;
say $mojo->res->code;
return get('http://metacpan.org');
})->then(sub {
my $cpan = shift;
say $cpan->res->code;
})->catch(sub {
my $err = shift;
warn "Something went wrong: $err";
})->wait;
# Synchronize non-blocking operations (all)
my $mojo = get('http://mojolicious.org');
my $cpan = get('http://metacpan.org');
Mojo::Promise->all($mojo, $cpan)->then(sub {
my ($mojo, $cpan) = @_;
say $mojo->[0]->res->code;
say $cpan->[0]->res->code;
})->catch(sub {
my $err = shift;
warn "Something went wrong: $err";
})->wait;
# Synchronize non-blocking operations (race)
my $mojo = get('http://mojolicious.org');
my $cpan = get('http://metacpan.org');
Mojo::Promise->race($mojo, $cpan)->then(sub {
my $tx = shift;
say $tx->req->url, ' won!';
})->catch(sub {
my $err = shift;
warn "Something went wrong: $err";
})->wait;
=head1 DESCRIPTION
L<Mojo::Promise> is a Perl-ish implementation of
L<Promises/A+|https://promisesaplus.com>.
=head1 ATTRIBUTES
L<Mojo::Promise> implements the following attributes.
=head2 ioloop
my $loop = $promise->ioloop;
$promise = $promise->ioloop(Mojo::IOLoop->new);
Event loop object to control, defaults to the global L<Mojo::IOLoop> singleton.
=head1 METHODS
L<Mojo::Promise> inherits all methods from L<Mojo::Base> and implements
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.
=head2 catch
my $new = $promise->catch(sub {...});
Appends a rejection handler callback to the promise, and returns a new
L<Mojo::Promise> object resolving to the return value of the callback if it is
called, or to its original fulfillment value if the promise is instead
fulfilled.
# Longer version
my $new = $promise->then(undef, sub {...});
# Pass along the rejection reason
$promise->catch(sub {
my @reason = @_;
warn "Something went wrong: $reason[0]";
return @reason;
});
# Change the rejection reason
$promise->catch(sub {
my @reason = @_;
return "This is bad: $reason[0]";
});
=head2 finally
my $new = $promise->finally(sub {...});
Appends a fulfillment and rejection handler to the promise, and returns a new
L<Mojo::Promise> object resolving to the original fulfillment value or rejection
reason.
# Do something on fulfillment and rejection
$promise->finally(sub {
my @value_or_reason = @_;
say "We are done!";
});
=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.
=head2 reject
$promise = $promise->reject(@reason);
Reject the promise with one or more rejection reasons.
=head2 resolve
$promise = $promise->resolve(@value);
Resolve the promise with one or more fulfillment values.
=head2 then
my $new = $promise->then(sub {...});
my $new = $promise->then(sub {...}, sub {...});
my $new = $promise->then(undef, sub {...});
Appends fulfillment and rejection handlers to the promise, and returns a new
L<Mojo::Promise> object resolving to the return value of the called handler.
# Pass along the fulfillment value or rejection reason
$promise->then(
sub {
my @value = @_;
say "The result is $value[0]";
return @value;
},
sub {
my @reason = @_;
warn "Something went wrong: $reason[0]";
return @reason;
}
);
# Change the fulfillment value or rejection reason
$promise->then(
sub {
my @value = @_;
return "This is good: $value[0]";
},
sub {
my @reason = @_;
return "This is bad: $reason[0]";
}
);
=head2 wait
$promise->wait;
Start L</"ioloop"> and stop it again once the promise has been fulfilled or
rejected, does nothing when L</"ioloop"> is already running.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
|