Question: how can I consume this array of Streams?
See original GitHub issueHello, 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:
- Created 6 years ago
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The reason why I said you didn’t need
toBuffer
was because you were making the secondary call tomyapi.com
, which already returned buffers. Now that you’re not doing that anymore, you need to addtoBuffer
(or its equivalent) back in.Change
filter
toThe server outputs when fetching the test
jl
user.I had to pass that data to a
new Buffer
, otherwise, I’d get aTypeError: 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.