You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.4 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const pipe = require("@promistream/pipe");
const map = require("@promistream/map");
const spy = require("@promistream/spy");
const collect = require("@promistream/collect");
const fromIterable = require("@promistream/from-iterable");
const forkMirror = require("./");
return Promise.try(() => {
let [ streamA, streamB ] = pipe([
fromIterable([ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k" ]),
forkMirror(2, { bufferSize: Infinity, leaderMode: false }) // NOTE: Change `leaderMode` to see the output order change
]).read();
return Promise.all([
pipe([
streamA,
map((string) => {
return Promise
.resolve(`[stream A] ${string}`)
.delay(20); // Emulate some slow processing step
}),
spy((value) => console.log(value)),
collect()
]).read(),
pipe([
streamB,
map((string) => `[stream B] ${string}`),
spy((value) => console.log(value)),
collect()
]).read(),
]);
}).then((results) => {
console.log(results);
/*
[
[
'[stream A] a',
'[stream A] b',
'[stream A] c',
'[stream A] d',
'[stream A] e',
'[stream A] f',
'[stream A] g',
'[stream A] h',
'[stream A] i',
'[stream A] j',
'[stream A] k'
],
[
'[stream B] a',
'[stream B] b',
'[stream B] c',
'[stream B] d',
'[stream B] e',
'[stream B] f',
'[stream B] g',
'[stream B] h',
'[stream B] i',
'[stream B] j',
'[stream B] k'
]
]
*/
});