Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created April 20, 2014 21:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnthn/11126125 to your computer and use it in GitHub Desktop.
Save jnthn/11126125 to your computer and use it in GitHub Desktop.
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