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.

Promise.promisify fails on fs.createReadStream

See original GitHub issue

Hi,

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:closed
  • Created 9 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
manastcommented, Jan 25, 2017

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:

function createReadStream(filename){
  return new Promise(function(resolve, reject){
    function onError(err){
      reject(err);
    }

    function onReadable(){
      cleanup();
      resolve(stream);
    }

    function cleanup(){
      stream.removeListener('readable', onReadable);
      stream.removeListener('error', onError);
    }

    var stream = fs.createReadStream(filename);
    stream.on('error', onError);
    stream.on('readable', function(){
      resolve(stream);
    });
  });
}

@petkaantonov have you give any thought about a proper way to make streams work well together with promises?

2reactions
danjl1100commented, Mar 28, 2020

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)

function createReadStream(filename){
  return new Promise(function(resolve, reject){
    function onError(err){
      reject(err);
    }

    function onReadable(){
      cleanup();
      resolve(stream);
    }

    function cleanup(){
      stream.removeListener('error', onError);
      stream.removeListener('readable', onReadable);
    }

    var stream = fs.createReadStream(filename);
    stream.on('error', onError);
    stream.on('readable', onReadable);    // <-- small correction
  });
}

Expanded for the Writable case:

function createWriteStream(filename){
  return new Promise(function(resolve, reject){
    function onError(err){
      reject(err);
    }

    function onOpen(){
      cleanup();
      resolve(stream);
    }

    function cleanup(){
      stream.removeListener('error', onError);
      stream.removeListener('open', onOpen);
    }

    const stream = fs.createWriteStream(filename);
    stream.on('error', onError);
    stream.on('open', onOpen);
  });
}
Read more comments on GitHub >

github_iconTop 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 >

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