Streams and promises
See original GitHub issueI’ve a question on best practices when working with Promises and streams, with specific regards in how to “promisify” them. The example problem I’m trying to solve is with an API that returns a file stream. I then want to use Promise.map with concurrency on a large list of files, so that only c
files are streaming at any given time. I’ve solved this to my needs below:
Promise.map(files, function(file){
return new Promise(function(resolve, reject) {
var stream = API.getStream(file);
// Pipe/use stream
stream.pipe(endPoint);
stream.on('end', resolve);
stream.on('error', reject);
});
}, {concurrency: 3});
Main question is, is there a more semantic way to accomplish this? I’ve never before had to write new Promise(…) myself, so I have an instinctual feeling I’m missing something. On the other hand, I’m pretty sure it doesn’t make sense to automatically promisify API.getStream.
- The stream is returned synchronously already
- You don’t want a promise of a stream, you want a promise of a “finished” stream.
- You want the underlying stream for piping and other stream specific functions
- There’s no guarantee that an input is a stream, and not just a general EventEmitter style object.
An example where the API returns a promise of a finished stream.
Promise.map(files, function(file){
var streamPromise = API.getFinishedStreamAsync(file);
streamPromise.pipe(streamPromise.stream); // Stream is attached synchronously
// API now handles stream/promise resolution
return streamPromise;
}, {concurrency: 3});
Issue Analytics
- State:
- Created 9 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Using promises with streams in node.js - Stack Overflow
In this line stream.on("end", resolve(stream.dests[0].path));. you are executing resolve immediately, and the result of calling resolve ...
Read more >Promise, Observable vs Stream - Level Up Coding
Both Promises and Observables help us to work with the asynchronous functionalities in JavaScript. Promises are values that will resolve in asynchronous ways ......
Read more >Wrap your streams with promises for fun and profit
Streams are everywhere in node, and once you get the hang of using them, they are delightful. There are many solid articles that...
Read more >Stream | Node.js v19.3.0 Documentation
The stream/promises API provides an alternative set of asynchronous utility functions for streams that return Promise objects rather than using callbacks. The ...
Read more >Overview of Promise-Based APIs in Node.js - Future Studio
The stream/promises module provides two promise-supporting methods: - finished : waits for a stream to finish reading, writing or an error ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
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
It is generic code that can be tucked away in a utility function so it doesn’t clutter application code and obstruct actual application logic.
In your example simplest utility function would be like
And then the application code is simply
A small addition to @petkaantonov’s solution, suggest adding
stream.resume();
, otherwise there is no guarantee that the stream will drain and emitend
.Example:
but the print will happen if one uses: