Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created April 20, 2014 21:49
Show Gist options
  • Select an option

  • Save jnthn/11126125 to your computer and use it in GitHub Desktop.

Select an option

Save jnthn/11126125 to your computer and use it in GitHub Desktop.

Revisions

  1. jnthn created this gist Apr 20, 2014.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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', '/');