Source: promise.js

'use strict';

/**
 * Helper method that can be bound to a stream - it sets the output to utf-8, captures all of the results, and returns a promise that resolves to the final text.
 * Essentially a smaller version of concat-stream wrapped in a promise
 *
 * @param [stream=] optional stream param for when not bound to an existing stream instance
 */
module.exports = function promise(stream) {
  stream = stream || this;
  return new Promise(function (resolve, reject) {
    var results = [];
    stream.setEncoding('utf8')
    .on('data', function (result) {
      results.push(result);
    }).on('end', function () {
      resolve(results.join(''));
    }).on('error', reject);
  });
};