Construct a Readable stream from a thenable. Useful if you don't have the actual stream yet.
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.jshintignore
.jshintrc
.npmignore
.travis.yml
LICENSE.txt
README.md
package.json

README.md

thenstream

Construct a Readable stream from a thenable. Useful if you don't have the actual stream yet.

Deprecation Notice

This module is no longer maintained. You can achieve a similar effect like this:

'use strict'

const PassThrough = require('stream').PassThrough

function streamify (thenable, opts) {
  const stream = new PassThrough(opts)

  Promise.resolve(thenable)
    .then(input => input.pipe(stream))
    .catch(err => stream.emit('error', err))

  return stream
}

This won't fully replace this module but it should suffice for most use cases.

Installation

npm install thenstream

Usage

With Legendary installed:

var Thenstream = require('thenstream');
var Promise = require('legendary').Promise;

var p = new Promise(function(resolve) {
  setTimeout(function() {
    resolve(getReadableStreamSomehow());
  }, 2000);
});

var readable = new Thenstream(p)
readable.on('readable', function() {
  var chunk;
  while (null !== (chunk = readable.read())) {
    console.log('got %d bytes of data', chunk.length);
  }
});

API

thenstream exports a single class, Thenstream.

new thenstream.Thenstream(thenable)

Subclass of stream.Readable. Can be called without new.

The returned stream is immediately readable, but no data will be available until thenable fulfils with a readable stream. The thenable is assimilated along the lines of the Promise Resolution Procedure. Any error in assimilation will be emitted (after the constructor has returned) as an error event. Similarly an error event will be emitted if the thenable does not fulfil with something that looks like a readable stream.

Note: thenables-for-thenables are not supported. If you need to handle such thenables you should wrap them using your promise library of choice, prior to creating the thenstream. Legendary would be a great choice.