From ce14514cbf4c6051f99d3c5a594459c236cdb0c5 Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Tue, 13 Oct 2015 17:21:38 +1100 Subject: [PATCH] If `callback` is not defined it returns a promise. --- README.md | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f41e234..e71205f 100644 --- a/README.md +++ b/README.md @@ -42,22 +42,27 @@ stream.toArray(function (err, arr) { }) ``` -If `callback` is not defined, then it is assumed that it is being yielded within a generator. +If `callback` is not defined, then it returns a promise. ```js -function* () { - var stream = new Stream.Readable() - stream.toArray = toArray - var arr = yield stream.toArray() -} +toArray(stream) + .then(function (parts) { + + }) ``` If you want to return a buffer, just use `Buffer.concat(arr)` ```js -var stream = new Stream.Readable() -var arr = yield toArray(stream) -var buffer = Buffer.concat(arr) +toArray(stream) + .then(function (parts) { + var buffers = [] + for (var i = 0, l = parts.length; i < l ; ++i) { + var part = parts[i] + buffers.push((part instanceof Buffer) ? part : new Buffer(part)) + } + return Buffer.concat(buffers) + }) ``` [npm-image]: https://img.shields.io/npm/v/stream-to-array.svg?style=flat-square