Skip to content

Instantly share code, notes, and snippets.

@maros
Created July 21, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maros/d9f5a8d1cc6d96f374f0c45e22e7ab0a to your computer and use it in GitHub Desktop.
Save maros/d9f5a8d1cc6d96f374f0c45e22e7ab0a to your computer and use it in GitHub Desktop.
perl5:
use EV;
use Promises backend => ['EV'], qw(deferred);
use AnyEvent;
my $d1 = deferred();
my $final = $d1->promise->then(sub($r) {
my $d2 = deferred();
say 'GOT '.$r; # Should be TEST1
my $timer;
$timer = AnyEvent->timer(
after => 2,
cb => sub {
$timer = undef;
$d2->resolve;
}
);
return $d2->promise;
})->then(sub($r) {
say 'GOT '.$r; # Should be TEST2
});
$d1->resolve('TEST1');
my $c = AnyEvent->condvar;
collect($final)
->done(
sub { $c->send(@_); },
sub { $c->croak(@_); },
);
$c->recv;
javascript:
var p1 = new Promise(function(resolve) { resolve('Test1') });
await p1.then((r) => {
console.log('GOT '+r);
return new Promise(function(resolve) { setTimeout(function(){ resolve('Test2') },2000) })
})
.then((r) => {
console.log('GOT '+r)
});
console.log("DONE")
raku:
my $p1 = Promise.new();
$p1.vow.keep("TEST1");
await $p1.then({
say "GOT " ~ .result; # expecting TEST1
return Promise.in(2).then: { return "TEST2" };
}).then({
say $_.gist; # Promise, NOT "TEST2"
say "GOT" ~ .result; # expecting TEST2, but got exception
});
say "DONE";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment