diff --git a/src/PureStream.spec.ts b/src/PureStream.spec.ts index d8a5df8..f575522 100644 --- a/src/PureStream.spec.ts +++ b/src/PureStream.spec.ts @@ -62,7 +62,7 @@ describe('PureStream', () => { }); describe('wrap', () => { - it('wraps native stream', (done) => { + it('wraps node stream', (done) => { const source = new PassThrough({ objectMode: true }); const dest = new PureStream(); @@ -77,7 +77,7 @@ describe('PureStream', () => { source.destroy(new Error('test')); }); - it('wraps native stream with multiple errors', (done) => { + it('wraps node stream with multiple errors', (done) => { const source = new PassThrough({ objectMode: true }); const dest = new PureStream(); diff --git a/src/PureStream.ts b/src/PureStream.ts index 60c30bb..e2ed8b0 100644 --- a/src/PureStream.ts +++ b/src/PureStream.ts @@ -205,6 +205,18 @@ export class PureStream { return stream; } + /** Convert this PureStream into a promise for an array of each value in the stream */ + public toPromise() { + return new Promise((resolve, reject) => { + const accumulated: Out[] = []; + + this.each((value) => accumulated.push(value)).done((err) => { + if (err) reject(err); + else resolve(accumulated); + }); + }); + } + public static wrap(source: Readable): PureStream; public static wrap(source: PassThrough): PureStream; public static wrap(source: Transform): PureStream; diff --git a/src/methods/to.ts b/src/methods/to.ts index 9e9e66f..483c1ab 100644 --- a/src/methods/to.ts +++ b/src/methods/to.ts @@ -1,19 +1,27 @@ -import { reduce } from "./reduce"; -import { PureStream } from "../PureStream"; +import { reduce } from './reduce'; +import { PureStream } from '../PureStream'; /** * Convert a stream into a promise for an array of values. * This consumes the stream. */ export function toPromise(stream: PureStream) { + // eslint-disable-next-line no-console + console.trace( + 'Call to deprecated method `toPromise(stream)`; please use `PureStream.prototype.toPromise()`' + ); + return new Promise((resolve, reject) => { - stream.pipe(reduce(async (prev, next) => { - prev.push(next); - return prev; - }, [])) + stream + .pipe( + reduce(async (prev, next) => { + prev.push(next); + return prev; + }, []) + ) .each(resolve) .done((err) => { if (err) reject(err); - }) + }); }); }