0.1.0 • Published 10 years ago
promisify-stream v0.1.0
Promisify Stream
Wrap a node stream to use Promises instead of events. Useful for use with generator-based control flow libraries such as co.
Usage
npm i promisify-stream
var wrap = require('promisify-stream')
var co = require('co')
var fs = require('fs')
var stream = wrap(fs.createReadStream('some-file.txt'))
co(function* () {
// internal stream instance
stream.stream.setEncoding('utf8')
var buf
while (buf = yield stream.read()) {
console.log(buf)
}
})
API
stream.read(size).then((chunk) ->)
Read the next chunk in the stream.
stream.write(chunk, encoding).then((writable) ->)
Write the next chunk in the stream.
If writable
is false
, then you should stop writing as something went wrong.
stream.end(chunk, encoding).then()
.end()
the stream, optionally with a chunk
,
and wait until the finish
event is emitted.
stream.destroy()
The stream's .destroy()
method, if any
stream.stream
The node stream instance
0.1.0
10 years ago