Create a gist now

Instantly share code, notes, and snippets.

sub crappy_async_lwp($host, $path) {
my $p = Promise.new;
my $v = $p.vow;
IO::Socket::Async.connect($host, 80).then(-> $sr {
if $sr.status == Kept {
my $socket = $sr.result;
$socket.send("GET $path\r\n\r\n").then(-> $wr {
if $wr.status == Broken {
$v.break($wr.cause);
$socket.close();
}
});
my @chunks;
$socket.chars_supply.tap(
{ @chunks.push($_) },
done => {
$socket.close();
$v.keep(@chunks.join());
},
quit => { $v.break($_); });
}
else {
$v.break($sr.cause);
}
});
$p;
}
say await crappy_async_lwp('www.jnthn.net', '/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment