<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: nic</title>
    <description>The latest articles on Forem by nic (@nicferrier).</description>
    <link>https://forem.com/nicferrier</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F131066%2Fb241bd0e-74d6-4bdf-808f-197c484e9be2.jpg</url>
      <title>Forem: nic</title>
      <link>https://forem.com/nicferrier</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nicferrier"/>
    <language>en</language>
    <item>
      <title>http requests received turned into an Iterator</title>
      <dc:creator>nic</dc:creator>
      <pubDate>Sat, 15 Feb 2020 08:11:52 +0000</pubDate>
      <link>https://forem.com/nicferrier/http-requests-received-turned-into-an-iterator-g47</link>
      <guid>https://forem.com/nicferrier/http-requests-received-turned-into-an-iterator-g47</guid>
      <description>&lt;p&gt;There are a few situations where this sort of thing is useful and I have an important one at work right now.&lt;/p&gt;

&lt;p&gt;I thought this was hard at first and I looked at a few modules for doing it... but then I slept on it and ... ta da!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import fetch from "node-fetch";
import {Response} from "node-fetch";
import * as express from "express";
import {EventEmitter} from "events";

async function* gt() {
    const app = express();
    const eventThing = new EventEmitter();
    app.post("/", function (request, response) {
        response.sendStatus(204);
        eventThing.emit("helloEvent", {
            token: ["hello", "world"]
        });
    });
    app.listen(8000);

    const queueEvents = new EventEmitter();
    const queue:any[] = [];
    eventThing.on("helloEvent", e =&amp;gt; {
        queue.push(e);
        queueEvents.emit("queueReady", queue);
    });

    const response:Response = await fetch("http://localhost:8000/", {
        method: "POST"
    });
    setInterval(async _ =&amp;gt; {
        const response:Response = await fetch("http://localhost:8000/", {
            method: "POST"
        })
    }, 6000);

    while (true) {
        while (queue.length &amp;gt; 0) {
            yield queue.pop();
        }
        await new Promise((resolve, reject) =&amp;gt; {
            queueEvents.once("queueReady", resolve);
        });
    }
}

async function test() {
    const stream = gt();
    let next = await stream.next();
    while (!next.done) {
        console.log(next.value);
        next = await stream.next();
    }
}

test().then();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I guess you could make it an IterableIterator too.&lt;/p&gt;

&lt;p&gt;I am not sure this is perfect. Would I lose events between the end of the queue in the while loop and the next queue.once? I'm not sure. Anyone wanna tell me?&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>http</category>
    </item>
  </channel>
</rss>
