Find a file
2021-03-02 12:33:24 +01:00
.gitignore Initial commit 2020-07-20 23:06:56 +02:00
example.js Initial commit 2020-07-20 23:06:56 +02:00
index.js Update implementation to new simple-sink API 2021-03-02 12:33:24 +01:00
package.json Initial commit 2020-07-20 23:06:56 +02:00
README.md Initial commit 2020-07-20 23:06:56 +02:00
yarn.lock Initial commit 2020-07-20 23:06:56 +02:00

@ppstreams/collect

This is a sink stream that's used to collect the results of a pipeline into a single array.

When its read method is called, this stream returns a Promise and then just endlessly reads from its source stream until it receives an EndOfStream. Once the source stream ends, the Promise is resolved with an array containing every value that the collect stream has received.

Example

"use strict";

const Promise = require("bluebird");
const pipe = require("@ppstreams/pipe");
const rangeNumbers = require("@ppstreams/range-numbers");
const collect = require("@ppstreams/collect");

return Promise.try(() => {
	return pipe([
		rangeNumbers(0, 20),
		collect()
	]).read();
}).then((result) => {
	console.log(result);
	// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
});

API

collect()

Returns a new collect stream. Takes no arguments.

Stream behaviour:

  • Reads from source: Infinitely, until the end of the source stream is reached.
  • Consumes: Values of any kind.
  • Produces: Returns a Promise when .read() is called, that will resolve with accumulated values received from the source stream when its end is reached, or reject when an error of any kind (including an Aborted marker) is received from the source stream.
  • Throws: Only when an error is received from upstream.
  • Aborts: Never.