node-awaitify-stream
Read or write to a stream using while
and await
, not event handlers.
- Read and write streams using familiar constructs, without resorting to synchronous methods for I/O.
- Read and write long streams without runaway memory usage.
- Process streams one chunk or line at a time. Await asynchronous operations inbetween.
Requirements
- Node v8+ recommended.
async
/await
support was added to Node v7. Node v6 will throw "SyntaxError: Unexpected token function" for the examples below. See secondExample_without_await.js for an example of using awaitify-stream withoutawait
.
Install
npm install awaitify-stream
Reader functions
readAsync([size])
: Promise wrapper around readable.read. Returns a promise for the next chunk of data. Resolves to null at the end of the stream.
Writer functions
-
writeAsync(chunk[, encoding])
: Promise wrapper around writable.write. Returns a promise that resolves following adrain
event (if necessary) and a call towrite
. Doesn't wait for the chunk to be flushed. -
endAsync([chunk][, encoding])
: Promise wrapper around writable.end. Returns a promise that resolves when the stream is finished.
Reader/Writer API
Use createReader
, createWriter
or createDuplexer
to create a wrapper around the stream. The stream
property can be used to later access the stream.
const fs = ;const aw = ; { let readStream = fs; let reader = aw; let writer = aw; // Read the file and write it to stdout. let chunk count = 0; while null !== chunk = await reader // Perform any synchronous or asynchronous operation here. await writer; count++; console;} ;
Augment Stream API
Use addAsyncFunctions
to add the reader and/or writer functions to a stream object. The reader functions are added if stream.readable
is true. The writer functions are added if stream.writable
is true.
const fs = ;const aw = ;const lineLength = 6; // 5 digits in a zip code, plus the newline character. { return { ; };} { let stream = aw; stream; // Read and print zip codes, slowly. let zipCode; while null !== zipCode = await stream // Remove the newline character. If you didn't set the encoding // above, use zipCode.toString().trim() zipCode = zipCode; console; await ; } ;
Line Reading
You can use awaitify-stream
in combination with a package like byline
to read a line at a time.
const fs = ;const aw = ;const byline = ;const readline = ; // Used for prompting the user. { return { rl; };} { let stream = fs; stream; let lineStream = byline; let reader = aw; const rl = readline; try let line; while null !== line = await reader let guessedCard = await ; if guessedCard console; return; console; finally rl; } ;
Related Packages
-
byline: Useful for reading streams line-by-line. I recommend using
byline
over node's builtin readline because you can pause the stream or await asynchronous operations inbetween each line. -
stream-consume-promise: Similar to this package, but returns an iterator-style response with
value
anddone
properties. Also see stream-produce-promise.
Notes
- The library has no dependencies.
mocha
andbyline
are required only for testing.
Credits
byline served as an example package as I was writing awaitify-stream
, my first package.
davedoesdev contributed fixes.
mknj identified an issue with error handling.