question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how can I consume this array of Streams?

See original GitHub issue

Hello, nice folks!

I have a function that makes a request and returns a stream.

const getUser = (userId) => request(`https://www.myapi.com/users?id=${userId}`)

I have an array of users’ IDs and I want to call getUser on each one of them:

const users = [
  159753,
  753159,
  654789,
  159753,
  753368
]

*Most of the time I don’t know how many users I will have in my array.

Now I want to create an array of streams:

const requests = users.reduce((arr, user) => [...arr, getUser(user)], [])

What I am trying to do is iterate over requests and pipe its response to http.ServerResponse:

app.get('/', (req, res) => {
  _(requests).pipe(res)
})

When I do this I get an error: Stream already being consumed, you must either fork() or observe()

What I tried is something like _(requests.fork()).pipe(res) but it obviously didn’t work.

It might be something silly that I’m missing but if anyone could point me to what am I doing wrong, or have any hint, I’d really appreciate.

Thank you!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vqvucommented, Oct 25, 2017

The reason why I said you didn’t need toBuffer was because you were making the secondary call to myapi.com, which already returned buffers. Now that you’re not doing that anymore, you need to add toBuffer (or its equivalent) back in.

Change filter to

const filter = (stream) => stream.map(parseJSON).map(filterSubmissions).map(JSON.stringify);

The server outputs when fetching the test jl user.

[14237416,11871616,11483492]
0reactions
viniciusCamargocommented, Oct 25, 2017

I had to pass that data to a new Buffer, otherwise, I’d get a TypeError: First argument must be a string or Buffer. I refactored the code and changed some functions names to help, but it still doesn’t work.

const express = require('express')
const rp = require('request-promise')
const _ = require('highland')

const app = express()

const parseJSON = (buffer) => JSON.parse(buffer.toString())

const filterSubmissions = (data) => {
  const { submitted } = data

  return submitted.filter((e, i) => i < 3) // returns the first 3 entries
}

const filter = (stream) => stream.map(parseJSON).map(filterSubmissions)

const getUser = (id) => `https://hacker-news.firebaseio.com/v0/user/${id}.json?print=pretty`

app.get('/', (req, res) => {
  _(rp(getUser('user'))).through(filter).pipe(res)
})

app.listen(3000, () => console.log('http://localhost:3000'))

Read more comments on GitHub >

github_iconTop Results From Across the Web

Streams on Arrays in Java 8 - GeeksforGeeks
In this article, we would be going through stream method of Arrays class which is added in Java 8, it simplifies many operations...
Read more >
Java 8 Stream and operation on arrays - Stack Overflow
Arrays to convert an array into a Java 8 stream which can then be used for summing etc. int sum = Arrays.stream(myIntArray).sum();.
Read more >
3 Ways to Convert Java 8 Stream to an Array - Javarevisited
Though this method will convert the Stream to an array it has a problem, it returns an Object array. What will you do,...
Read more >
arrays.stream() method in Java - Educative.io
The stream(T[] array) method of the Arrays class in Java returns a sequential stream of the array passed as the argument. The parameter...
Read more >
Java 8 Stream - DigitalOcean
There are several ways through which we can get a Collection or Array from a java Stream. We can use java Stream collect()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found