0.2.0 • Published 3 years ago

make-event-iterator v0.2.0

Weekly downloads
54
License
MIT
Repository
github
Last release
3 years ago

Make Event Iterator

Do you have event emitters you are wrapping in a single promise? Is the return from that process massive and causing memory issues? Do you want an excuse to use for await...of?

Create an iterator from an event emitter!

Installation

npm i make-event-iterator

Usage

It is easier than saying, just supply the an event emitter to the package.

const makeEventIterator = require('make-event-iterator');
for await(const chunk of makeEventIterator(eventEmitter)) {
    // ...
}

http server

const http = require('http');
const makeEventIterator = require('make-event-iterator');

async function handleHttp(req, res) {
    for await(const chunk of makeEventIterator(req)) {
        console.log('chunk:', chunk.length);
    }
    res.end();
}

const server = http.createServer(handleHttp);
server.listen(8000);

readline

const fs = require('fs');
const readline = require('readline');
const makeEventIterator = require('make-event-iterator');

const fileStream = fs.createReadStream('file.txt');
const rl = readline.createInterface({
    input: fileStream,
});

for await (const line of makeEventIterator(rl, makeEventIterator.READLINE)) {
    console.log('line:', line);
}

License

Copyright (c) 2021, Michael Szmadzinski. (MIT License)

0.2.0

3 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago