promisify-stream

promisify a node stream

Usage no npm install needed!

<script type="module">
  import promisifyStream from 'https://cdn.skypack.dev/promisify-stream';
</script>

README

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