Skip to content

Instantly share code, notes, and snippets.

/asyncawait.diff Secret

Created November 4, 2017 19:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/9e1f4858d7aa4e2ddb858147f4f6798f to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Promise.pm b/lib/Mojo/Promise.pm
index 42127f51e..6018e5ce1 100644
--- a/lib/Mojo/Promise.pm
+++ b/lib/Mojo/Promise.pm
@@ -6,6 +6,13 @@ use Scalar::Util qw(blessed weaken);
has ioloop => sub { Mojo::IOLoop->singleton };
+# "async/await"
+sub done { shift->resolve(@_) }
+sub fail { shift->reject(@_) }
+sub get { @{shift->{result} || []} }
+sub is_ready { !!shift->{result} }
+sub on_ready { shift->then(shift) }
+
sub all {
my ($class, @promises) = (ref $_[0] ? (undef, @_) : @_);
Mojo::Promise=HASH(0x7f9a1d496378) at ../../test.pl line 12.
reject: [
"TODO: handle cx->blk_eval.old_eval_root at ../../test.pl line 9.\n"
]
destroying object with 1 non-received or timed out results during global destruction.
use Mojo::Base -strict;
use Mojo::Promise;
use Mojo::UserAgent;
use Future::AsyncAwait;
my $ua = Mojo::UserAgent->new;
async sub foo {
await $ua->get_p('mojolicious.org');
await $ua->get_p('metacpan.org');
}
my $promise = foo();
warn $promise;
$promise->then(sub { warn 'resolve: ' . Mojo::Util::dumper [@_] })
->catch(sub { warn 'reject: ' . Mojo::Util::dumper [@_] })->wait;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment