/asyncawait.diff Secret
Created
November 4, 2017 19:19
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, @_) : @_); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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