Node-fs-extra: Does not promisfy createReadStream/createWriteStream

Created on 26 Jun 2018  路  3Comments  路  Source: jprichardson/node-fs-extra

Example:

import  fs  from 'fs-extra';
import * as http from 'http';

http.createServer(function (req, res) {

    res.writeHead(200, {'Content-Type': 'application/json'});

    fs.createReadStream('./sample.json.js')
     .then((data) => {

        console.log(data);
     }).catch((err) => {

        console.log(err);
    })
}).listen(3000);

would output
fs.createReadStream(...).then is not a function

promises question

Most helpful comment

Streams don't use callbacks by default, so they're not reasonably promisifyable. This is intentional.

All 3 comments

Streams don't use callbacks by default, so they're not reasonably promisifyable. This is intentional.

Totally reasonable, but would be kinda magical to abstract a promise interface for streams. Useful in simple contexts. But I completely understand why you wouldn't want to. Just tossing in my 2 cents.

Certain libraries return NodeStreams which are hard to handle with fs-extra... they shouldn't be.
When they aren't done they causing subtle annoying bugs. I don't see what's unreasonable about the following:

https://stackabuse.com/reading-and-writing-json-files-with-node-js/
For WriteStreams:

const streamToFile = (inputStream, filePath) => {
  return new Promise((resolve, reject) => {
    const fileWriteStream = fs.createWriteStream(filePath)
    inputStream
      .pipe(fileWriteStream)
      .on('finish', resolve)
      .on('error', reject)
  })
}
Was this page helpful?
0 / 5 - 0 ratings