Promise.promisify fails on fs.createReadStream
See original GitHub issueHi,
I just wanted to checkout this library and played a little bit with the pomisify stuff but it does not work for all methods:
var Promise = require('bluebird');
var createReadStream = Promise.promisify(require('fs').createReadStream);
var p1 = createReadStream('app.js').then(console.log);
setTimeout(function() {
console.log(p1);
}, 1000);
The p1 gets never fulfilled, no error, no success. I did test it for createWriteStream and it did not work, either.
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Nodejs `fs.createReadStream` as promise - Stack Overflow
Something to be aware of. You've removed the line processing from the current version of the question so the stream is being read...
Read more >Take advantage of promise-based APIs in Node.js
You can use util.promisify() to wrap callback-based APIs in Node.js core. But did you know that Node.js provides several promise-based APIs ...
Read more >Promise.promisify - Bluebird.js
Returns a function that will wrap the given nodeFunction . Instead of taking a callback, the returned function will return a promise whose...
Read more >Reading streams with promises in Node.js - Human Who Codes
Because readStream() returns a promise, you can use await to call it, making it fit in with the rest of the fs.promises API....
Read more >Returning Promises with Promisfy in Node.js - Bits and Pieces
Compared to callback functions that require writing an error catch ... To access the Promisify in Node.js Util's module, you import the Util ......
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
Something to note here is that createReadStream can fail in the future, and even if you created the stream inside a promise chain the error will be reported uncatched (and your application die). This is quite unfortunate, for example in the case of trying to open a missing file, you get a stream and in the next tick you get an exception. You need to create a stream like this to avoid problems, although this is far from ideal:
@petkaantonov have you give any thought about a proper way to make streams work well together with promises?
Regret digging up an old/closed thread, but I found @manast 's example useful and informative.
Hoping this will help anyone else who stumbles upon it: (tiny correction at the end)
Expanded for the Writable case: